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
        )

)
5
  • 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... Commented Apr 21, 2011 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? Commented Apr 21, 2011 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. Commented Apr 21, 2011 at 22:36
  • 1
    Check the comments at php.net/manual/en/function.array-diff.php for multidimensional array diff. Commented Apr 21, 2011 at 22:40
  • What's the problem with array_diff? Commented Apr 21, 2011 at 22:41

3 Answers 3

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
        )

)
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

-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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.