1

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
)
1
  • Because you're asking "What's in this ($array1) array that isn't in the others" Commented Oct 28, 2014 at 11:17

8 Answers 8

1

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.

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

Comments

0

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

Comments

0

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

Comments

0

According to PHP Manual : https://www.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.

Comments

0

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.

Comments

0

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".

Comments

0

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! :)

Comments

0

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.

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.