php: array_diff_assoc and the order of parameters

by Pascal Opitz

published 11 April 2007

This took a while till I found out: They actually do matter!
Even worse: The manual says so!!!! Just that I was to stupid to read it.
Lesson learned. No more guessing, no more quick reading!

<?
$a = array(
    'x' => 'x',
    'y' => 'y',
    'z' => 'z',
    't' => 't',
);

$b = array(
    'x' => 'x',
    'y' => 'y',
    'z' => 'z',
    't' => 't',
    'g' => 'g',
);

print_r(array_diff_assoc($a, $b));
print_r(array_diff_assoc($b, $a));
?>

This will output:

Array
(
)
Array
(
    [g] => g
)

Comment

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

    array_diff_assoc() returns an array containing all the values from array1 that are not present in any of the other arguments.
    ShiningRay    12 April, 4:37am    #
  2. I know! Stupidity never dies, not even in oneself. However, I was assuming it would behave like a unix diff.
    Pascal Opitz    12 April, 9:57am    #

Have your say

If your comment doesn't show up straight away, please don't be offended - we're not censoring, we are just trying to keep out all the viagra and poker ads. Thanks to an explosion of spam all comments are held for manual green-lighting.

name Remember
email
http://
Message <?>

Quick links

Other people's articles that we think you might be interested in: