Question

I have an array in PHP $array which has elements like

$array['id'].. $array['name'] $array['class']

I have another array called $array1 which has element only $array1['uid']. I want to match these two array on the basis of $array['id'] and $array['uid'] such that I want to get elements $array['id'] not equal to $array['uid']` Is there any builtin function in PHP, I can do that in for each loop with my custome function , but is there any function?

Input is if $array has id=2,4,5,6 and $array has uid=2,4 then I should get $array id=5,6

Data in $array looks like this

{
      "name": "abc", 
      "id": "37402526"
    }, 
    {
      "name": "def", 
      "id": "506768590"
    }, 
    {
      "name": "hij", 
      "id": "526405977"
   }

And $array 1 like this

{

      "id": "37402526"
    }, 
    {

      "id": "506768590"
    }, 
    {
      "
      "id": "526405977"
   }
Was it helpful?

Solution

If you can rewrite your code, to have the id's in array keys, than you could use array_diff_key():

$array = array(
    '12' => array('name' => 'abc'),
    '34' => array('name' => 'def')
);

$array2 = array('12' => true);

$result = array_diff_key($array, $array2);

Otherwise you can use array_udiff():

function my_id_cmp($a, $b) {
    return strcmp($a['id'], $b['id']);
}

$result = array_udiff($array, $array1, 'my_id_cmp');

OTHER TIPS

If your input is just in the format you used as example, then it is simple :

$array['id'] = implode(',', array_diff(explode(',', $array['id']), explode(',', $array1['uid'])));

Regards.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top