2

I have two multidimensional arrays which are indexed arrays of associative rows.

$array1 = array(
    array('name' => 'foo', 'id' => 12),
    array('name' => 'bar', 'id' => 34),
    array('name' => 'boo', 'id' => 56),
);

$array2 = array(
    array('name' => 'bar', 'id' => 34),
    array('name' => 'boo', 'id' => 56),
    array('name' => 'bar', 'id' => 78),
);

It is possible that rows might have different id values but the same name value -- such as bar in my sample input data.

I need to compare the arrays based on id values only.

Expected Output: (because ids 34 and 56 are found in $array2)

array(
    array('name' => 'foo', 'id' => 12)
)

I tried $one_not_two = array_diff($array1['id'], $array2['id']); which 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, then just comparing those. Now a new feature in our application requires me to compare the rows while maintaining the multidimensional structure. Any advice?

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

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Isaac
  • 625
  • 1
  • 12
  • 30

5 Answers5

4

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

Community
  • 1
  • 1
Joren
  • 3,068
  • 25
  • 44
  • 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. – Isaac Nov 13 '13 at 21:21
  • Then you should look at this question: http://stackoverflow.com/questions/11821680/array-diff-with-multidimensional-arrays – Joren Nov 13 '13 at 21:29
  • Added your comment to the answer – Isaac Nov 19 '13 at 14:36
1

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

Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43
0

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;
}
0

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

Isaac
  • 625
  • 1
  • 12
  • 30
0

Having the same problem as you.

Solved it with: $result = array_diff_assoc($array2, $array1);

Reference: PHP: array_diff_assoc

AFwcxx
  • 467
  • 1
  • 4
  • 15