Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have two arrays of the form

Array1:
[0]=> Array([name] => foo [id] => 12)
[1]=> Array([name] => bar [id] => 34)

Array2:
[0]=>Array([name] => bar [id]=> 34)
[1]=>Array([name] => baz [id]=> 56)

The arrays come from a database and any two pairs can have the same name but ID's are unique. I am trying to compare the arrays by ID likes so:

$one_not_two = array_diff($array1[id], $array2[id]);
but that does not return anything. I also tried
$one_not_two = array_diff($array1[id], $array2[id]);
which returned an error "argument is not an array." Originally I got around it by extracting the IDs into a one-dimensional array and just comparing those but now a new feature requires me to compare the pairs. Any advice?

PS Our servers are running php 5.3 if that makes any difference.

share|improve this question
2  
What are you trying to achieve? End goal? –  Sajuna Fernando Nov 13 '13 at 21:12
add comment

4 Answers

up vote 2 down vote accepted

Because the arrays are multidimensional you have to extract the ids like this:

$ids1 = array();
foreach($array1 as $elem1)
    $ids1[] = $elem1['id'];

$ids2 = array();
foreach($array2 as $elem2)
    $ids2[] = $elem2['id'];

$one_not_two = array_diff($ids1,$ids2);

For your specific question, check out array_diff() with multidimensional arrays

share|improve this answer
    
Thanks but I mentioned in my question that I tried something similar already. That would be an easy solution but something in the rest of the script requires $one_not_two to contain the mapping name => id. –  Yitzchak Nov 13 '13 at 21:21
    
Then you should look at this question: stackoverflow.com/questions/11821680/… –  Joren Nov 13 '13 at 21:29
    
Added your comment to the answer –  Yitzchak Nov 19 '13 at 14:36
add comment

You could use array_udiff to create a custom diff function:

$diff = array_udiff($array1,
                    $array2,
                    function ($a, $b){
                        if($a['id'] == $b['id']){
                            return 0;
                        } else {
                            return ($a['id'] < $b['id'] ? -1 : 1);
                        }
                    }
                );

http://codepad.viper-7.com/2u5EWg

share|improve this answer
add comment

If id is unique you can do that:

$one_not_two = array();

foreach ($array1 as $val) {
    $one_not_two[$val['id']] = $val;
}

foreach ($array1 as $val) {
    if (!isset($one_not_two[$val['id']])) {
        $one_not_two[$val['id']] = $val;
}
share|improve this answer
add comment

In the end I solved it by changing Array1 to an associative array of name => id

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.