★ wanayoo — archive 1999 http://www.php.net/manual/it/function.array-diff.phpNouvelle recherche | Portail wanayoo
PHP Friday, April 20, 2001  
downloads | documentation | faq | support | reporting bugs | links 

search for in the  


previousarray_count_values
array_flipnext

Last updated: Sun, 08 Apr 2001
view this page in English | Brazilian Portuguese | Czech | Dutch | French | Hungarian | Italian | Japanese | Korean | Plain HTML

array_diff

(PHP 4 >= 4.0.1)

array_diff -- Computes the difference of arrays

Description

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

Array_diff() returns an array containing all the values of array1 that are not present in any of the other arguments. Note that keys are preserved.

Esempio 1. Array_diff() example


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

This makes $result have array ("blue");

See also array_intersect().

User Contributed Notes: array_diff
ks@ksweb.de
30-Aug-2000 04:44
'preserved' means the keys are taken from the first accurence of the elements.
The example is wrong:
$array1 = array ("a" => "green", "red", "blue");
$array2 = array ("b" => "green", "yellow", "red");
$result = array_diff ($array1, $array2);
print_r($result);
$your_result=array("blue");
print_r($your_result);

outputs:
Array ( [1] => blue ) Array ( [0] => blue )

the correct Array-Statement of $result is:
$result=array("","blue");

venaas@php.net
22-Oct-2000 06:30
$result will be array(1 => "blue").
There is only one element.

jzago@ifhamy.insa-lyon.fr
10-Nov-2000 10:05
--- code ---
print_r ($valeurs);
print_r ($dummy);
print_r (array_diff ($valeurs, $dummy));

--- output ---
Array ( [19] => 1 )
Array ( [0] => 1 )
Array ( )

Remember folks, it diffs the values, not the keys !

umer@ezboard.com
29-Nov-2000 08:16
when you are comparing

array1 = array ("blue", "red", "green", "yellow");

array2 = array ("blue", "orange", "white", "yellow", "black");

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

you get back

$result[0] = "";
$result[1] = "orange";
$result[2] = "white";
$result[3] = "";
$result[4] = "black";

odd huh.. and if you are depending on seeing if you got any thing back from both arrays.. then the best thing to do is to count($result); and go from there..

jllamas@bal.com.mx
15-Dec-2000 11:17
To umer@ezboard.com, the same problem ocurrs with array_unique. Try this solution provided by matthew_dean@hotmail.com. Use

array_values(array_unique($myArray));

or you can also use the one suggested by burno@33-24-36.com (I haven't tested it myself).

usort($myArray,"return -1");

I think this problem is present on all array functions, but will only be noticed with numerically indexed arrays.

php-general@lists.php.net
06-Apr-2001 12:58
SHEESH! Ok after about an hour and a half of looking at print_r() output and rewriting my own code, I figured it out - numeric indexes are "keys" too... meaning:

---CODE---
$blah = array("eat","my","shorts");
$fooey = array("my","shorts","eat","you");

$yay = array_diff($fooey,$blah);

print_r($yay);

---OUTPUT---

array [
[3] => "you"
]

This causes for() loops to produce unexpected results:

for ($i=0; $i!=count($yay); $i++)
{
// do something with $yay[$i];
}

This stinks IMO - even though in much of PHP's functionality, the cross-functionality between numeric and associative arrays is all good, this is kind of silly... maybe a third option is in order, NUM_INDEX or something?

nhawks@intercosmos.com
06-Apr-2001 01:05
Here's how I worked around my prior problem about array_diff() and numeric indexes being turned associative.

if (is_array($yay))
{
$yay = array_values($yay);
}

david@audiogalaxy.com
08-Apr-2001 04:12
Note that array_diff() considers the type of the array elements when it compares them.

If array_diff() doesn't appear to be working, check your inputs using var_dump() to make sure you're not trying to diff an array of integers with an array of strings.

previousarray_count_values
array_flipnext

Last updated: Sun, 08 Apr 2001
add note | about notes

show source | credits | stats | mirror sites:  

Copyright © 2001 The PHP Group
All rights reserved.
This mirror generously provided by: chek.com
Last updated: Fri Apr 20 15:15:20 2001 EDT