★ wanayoo — archive 1999 http://www.php.net/manual/nl/function.array-diff.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
English version of this pageGerman version of this page is not availableJapanese version of this page
Italian version of this pageFrench version of this pageHungarian version of this page
Spanish version of this page is not availableDutch version of this pageBrazilian Portuguese version of this page
Korean version of this pageCzech version of this page
Arrays
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unique
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* natsort
* natcasesort
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort

  Thanks to:
 Chek.com
 easyDNS
 VA Linux Systems

  Related sites:
Apache
MySQL
PostgreSQL
Zend Tech.

  Community:
LinuxFund.org
OSDN
Manual: array_diff
View the source code for this pageSearch the site



Previous page
 array_count_values
 Updated
Sun, 21 Jan 2001
array_flip 
Next page


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.

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



 About Notes


Previous page
 array_count_values
 Updated
Sun, 21 Jan 2001
array_flip 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.