0

I'm trying to find the difference in 2 arrays.

$inven_old = array(1,2,3,4); 
$inven_new = array(1,2,3,4,5); 
$result = array_diff($inven_old, $inven_new); 
print_r($result);

Why is the outcome nothing?

Shouldn't it be "5" ?

If not, how can I do what I'm trying to do?

6
  • try var_dump($result) to see it's structure Commented Apr 1, 2012 at 22:04
  • php.net/manual/en/function.array-diff.php Returns an array containing all the entries from array1 that are not present in any of the other arrays. Commented Apr 1, 2012 at 22:36
  • is there a command to do what I need then? Commented Apr 1, 2012 at 22:37
  • You can't; array_diff returns all the unique items in the arrays. In both your arrays, 5 and 4 are present, thus nothing in unique. array_diff does not care about the quantity of the elements in the array. Commented Apr 1, 2012 at 22:38
  • @user1022585: nope, there is no any built-in one Commented Apr 1, 2012 at 22:38

7 Answers 7

6

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

http://php.net/manual/en/function.array-diff.php

array_diff() gives the result of subtracting all arrays except the first, from the first. Thus, things which are in the second array but not the first won't show up. (In other words, it's not the symmetric difference.)

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

Comments

2

http://www.php.net/manual/en/function.array-diff.php

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

So if you do:

$result = array_diff($inven_new, $inven_old);

The result will contain "5".

Bonus: If you need something that works regardless of the order of the arrays, try:

$result = array_diff($a + $b, array_intersect($a, $b));

Comments

1

A very late response but maybe this will help someone sometime ..

function array_deep_diff($d1, $d2) {
    if (is_array($d1) && is_array($d2))  {
        $diff = array();
        foreach (array_unique(array_merge(array_keys($d1), array_keys($d2))) as $key) {
            if (!array_key_exists($key, $d1)) {
                $diff['added'][$key] = $d2[$key];
            } else if (!array_key_exists($key, $d2)) {
                $diff['removed'][$key] = $d1[$key];
            } else {
                $tmp = array_deep_diff($d1[$key], $d2[$key]);
                if (!empty($tmp)) $diff[$key] = $tmp;
            }
        }
        return $diff;

    } else if (!is_array($d1) && !is_array($d2))  {
        if ($d1 == $d2) return array();
        $ret = "$d1 ---> $d2";
        // just a little touch for numerics, might not be needed
        if (is_numeric($d1) && is_numeric($d2)) {
            if ($d1 > $d2) $ret .= ' [- ' . ($d1 - $d2) . ']';
            if ($d1 < $d2) $ret .= ' [+ ' . ($d2 - $d1) . ']';
        }
        return $ret;

    } else {
        return array('Array compared with NonArray');
    }       
}

Comments

0

to make it work as you expect you need to reverse arguments:

$result = array_diff($inven_new, $inven_old); 

Otherwise you are checking which elements are missing in the inven_new, and obsiously none is missing.

Comments

0

The second array should come first

$inven_old = array(1,2,3,4); 
$inven_new = array(1,2,3,4,5); 
$result = array_diff($inven_new, $inven_old); 
print_r($result);

Comments

0

There is no built-in function but you can easily write a custom one (http://ideone.com/UNRKV):

$old = array(5,5,4); 
$new = array(5,5,4,4); 

function array_subtract($a, $b)
{
    foreach ($b as $val) {
        $i = array_search($val, $a);
        if ($i !== false) {
            unset($a[$i]);
        }
    }

    return array_values($a);
}

$result = array_subtract($new, $old);

var_dump($result);

Comments

0

This consumes from the first array ($old) until all are gone:

$result = array();
$old = array_count_values($old);
foreach($new as $value)
{
    @$old[$value] ? $old[$value]-- : $result[] = $value;
}

Result:

$old = array(5,5,4);
$new = array(5,5,4,4);

...

array(1) {
  [0]=>
  int(4)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.