阵列Diff混淆

Upon trying this code, I am confused why the value yellow is not displayed.

Can someone elaborate this, please ?

    $array1 = array("a" => "green", "red", "blue", "red");
    $array2 = array("b" => "green", "yellow", "red");
    $result = array_diff($array1, $array2);

    print_r($result);

Multiple occurrences in $array1 are all treated the same way. This will output :

Array
(
    [1] => blue
)

The definition of

array_diff($array1, $array2); 

is to return entries in array1 that are not present in other arrays. You could do

$result2 = array_diff($array2, $array1);

and then merge $result1 and $result2 if you want to see what's missing in either one.

this happens because you are searching for the difference between the first and second array, which is just blue, because blue is not contained by the second array.

if you try this:

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array2, $array1);

print_r($result);

?>

the output will be Array ( [0] => yellow) because this is the difference between the second and first array.

Hope this helps! :D

Becouse this is how array_diff works:

Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

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

According to PHP Manual : http://fr2.php.net/manual/en/function.array-diff.php

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

Did you read the documentation?

Compares array1 against one or more other arrays and returns the values in array1 that are not present in any of the other arrays.

The only value in array1 which is not present in array2 is "blue".

Same thing confused me long time ago :) You should swap array in array_diff in your case. Arrray diff actually works: show me what i have in first array ($array1) that i don't have in other arrays Cheers! :)

From documentation:

array array_diff ( array $array1 , array $array2 [, array $... ] )

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

According to this, what follows will print blue because it is the only element that does not exist in $array2.

It will not print yellow because it checks for the elements that are present in $array1 but not in $array2, NOT vice versa:

<?php
 $array1 = array("a" => "green", "red", "blue", "red");
 $array2 = array("b" => "green", "yellow", "red");
 $result = array_diff($array1, $array2);
 print_r($result);
?>

Only blue from $array1 does not exist in $array2, so print it. Do not care about the elements that are present in $array2 but not in $array1, so yellow won't be displayed.

The output is correct because the function displays the values that are present in the first array but not in the second one. If you want to display yellow then you have to invert the positions of the arrays in your function. Hope it helps.