1

I need to get the difference between these two arrays, I've tried array_diff($array1,$array2) without success, any idea?

array1

Array
(
    [0] => Array
        (
            [status] => 61192106047320064
        )

    [1] => Array
        (
            [status] => 61185038284357632
        )

    [2] => Array
        (
            [status] => 61182890951720960
        )

)

array2

Array
(
    [0] => Array
        (
            [status] => 61185038284357632
        )

    [1] => Array
        (
            [status] => 61182890951720960
        )

)

thanks in advance.

greenbandit
  • 2,267
  • 5
  • 30
  • 44
  • Define difference do you need all differences or if array1[0] == array2[0] do you need to print the difference. Show some code you tried. Whats your algorithm for searching the arrays... – austinbv Apr 21 '11 at 22:33
  • How exactly do you define difference? Set difference, symmetric difference, must elements have same array posision, ...? What do you expect the result to be? – phimuemue Apr 21 '11 at 22:33
  • the first array is with fresh results, second one is for comparte the values not the index, i need to get difference between arrays and put result into db, expecting [status] => 61192106047320064 from fisrt array. – greenbandit Apr 21 '11 at 22:36
  • 1
    Check the comments at http://php.net/manual/en/function.array-diff.php for multidimensional array diff. – Felix Kling Apr 21 '11 at 22:40
  • What's the problem with array_diff? – Adam Byrtek Apr 21 '11 at 22:41
  • possible duplicate of [Why does array_diff on arrays of arrays return an empty array?](http://stackoverflow.com/questions/4955623/why-does-array-diff-on-arrays-of-arrays-return-an-empty-array) – Felix Kling Apr 21 '11 at 22:42

3 Answers3

2

According to array_diff,

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.

Therefore you cannot diff the second dimension of these arrays directly.

Instead, maybe you can extract the status values with array_map, save as two 1-dimensional arrays, and then array_diff. If you have multiple keys, use a for loop.

Reci
  • 4,099
  • 3
  • 37
  • 42
2

Maybe I'm misunderstanding, but can't you just do something like this for your specific problem?

$newStatuses = array();

foreach($array1 as $element1) {
    foreach($array2 as $element2) {
        if($element1['status'] == $element2['status']) {
            continue 2;
        }
    }

    $newStatuses[] = $element1;
}

Each element of $newStatuses will be an array with a 'status' element from array1 that was not in array2.

So, $newStatuses would be this:

Array
(
    [0] => Array
        (
            [status] => 61192106047320064
        )

)
Compeek
  • 909
  • 5
  • 13
-1

have a look at this code, its part of cakephp but you may be able to adapt / rip it out

https://github.com/cakephp/cakephp/blob/master/cake/libs/set.php#L792

and the docs

http://book.cakephp.org/view/1496/diff

dogmatic69
  • 7,574
  • 4
  • 31
  • 49