0

There are two associative arrays which i need to compare. But both arrays have different number of index keys.

The first array is exclude array and second array has list of user ids. What i need to achieve is that i need to return only items having user_id that are not in array 1. I used array_diff_assoc() but returned wrong array.

Array 1:

Array
(
    [0] => Array
        (
            [a] => Array
                (
                    [user_id] => 10080
                )

        )

    [1] => Array
        (
            [a] => Array
                (
                    [user_id] => 10074
                )

        )

)

Array 2:

Array
(
    [0] => Array
        (
            [a] => Array
                (
                    [mail_id] => 14
                    [user_id] => 10080
                    [error_status] => 0
                    [recipient_type] => I
                )

        )

    [1] => Array
        (
            [a] => Array
                (
                    [mail_id] => 14
                    [user_id] => 10059
                    [error_status] => 0
                    [recipient_type] => I
                )

        )

)

$result = array_diff_assoc($arr1, $arr2);

RESULT:

Array
(
    [0] => Array
        (
            [a] => Array
                (
                    [user_id] => 10080
                )

        )

)
Wolverine
  • 455
  • 3
  • 8
  • 26
  • I kinda get what you want, but if you can explain why you are doing this we might be able to advise better. Do you really need to make a second array of data from one you already have? consider the overhead used to search and create a new array might match or be more than just accessing the one array with all data. Also, have you looked at all the array function options on php.net? http://php.net/manual/en/ref.array.php – James Mar 19 '15 at 09:24
  • I just need to filter array2 based on array 1 using index "user_id"... – Wolverine Mar 19 '15 at 09:43

1 Answers1

0

You can use this function I found here and modified for you using arra_udiff.

function udiffCompare($a, $b)
{
    return $a['a']['user_id'] - $b['a']['user_id'];
}
$arrdiff = array_udiff($array2, $array1, 'udiffCompare');
print_r($arrdiff);
Community
  • 1
  • 1
Sachin Joshi
  • 119
  • 5
  • Hi, i tried $mail_recipients = array_udiff($mail_recipients, $mail_exclude_recipients,function($a,$b){ return $a['a']['user_id'] - $b['a']['user_id']; }); but not working – Wolverine Mar 19 '15 at 10:06
  • yup it worked when custom function is written as separate function i tried to write it as inline.... thanks man! – Wolverine Mar 19 '15 at 10:14