Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Array Functions : array_diff

array_diff

Computes the difference of arrays (PHP 4 >= 4.0.1, PHP 5)
array array_diff ( array array1, array array2 [, array ...] )

Compares array1 against array2 and returns the difference.

Examples

Example242.array_diff() example

<?php
$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
)


Notes

Note:

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

Note:

This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.

Warning:

This was broken in PHP 4.0.4!

Related Examples ( Source code ) » array_diff



Code Examples / Notes » array_diff

ds2u

Yes you can get rid of gaps/missing keys by using:
<?php
$result = array_values(array_diff($array1,$array2));
?>
But to drop the storage of void spaces (actually a line feed) which are irritatingly indexed when reading from files - just use difference:
<?php
$array = array ();
$array[0] = "\n";
$result = array_diff($result,$array);
?>
dst


kitchin

Until recently, the description said:
"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."
Now it says:
"Compares array1 against array2 and returns the difference."
Now it's not clear what the optional arguments after the first two do.  Also, the difference is not symmetric in its arguments (that is array_intersect).


colin

Undocumented return:  it appears this will return NULL if an error occurs (e.g., an argument is not an array) and is not caught.
<?
@array_diff(NULL, array(1));
@array_diff(array(1), NULL);
@array_diff();
?>
All return NULL.  (Note the "@")


white_phoenix

To: effectpenguin at antarctida dot ru
Re: interesting effect
<?php
function arraycpy(&$target,&$array)
{
if (!is_array($target)) {$target = array();}
foreach($array as $k=>$v) {if ($k != "GLOBALS") {$target[$k] = $v;}}
}
arraycpy($old,$GLOBALS);
// some actions with variables:
$homer = "beer";
arraycpy($new,$GLOBALS);
$diff = array_diff($new,$old);
var_dump($diff);
?>
array(1) {
 ["homer"]=>
 string(4) "beer"
}
Windows NT WPX_NB 5.1 build 2600 PHP/5.0.4


ylu

To skip empty values, simply use:
$result = array_values(array_diff($array1,$array2));


viking coder

To anybody wanting a double-sided array_diff - mentioned by rudigier at noxx dot at. Remember, array_diff gives you everything in the first array that isn't in the subsequent arrays.
$array1=array('blue','red','green');
$array2=array('blue','yellow','green');
array_merge(array_diff($array1, $array2),array_diff($array2, $array1));
Result
------
Array
(
   [0] => red
   [1] => yellow
)


tim

The description is wrong, array_diff() returns an array consisting of all elements in $array1 that are not in $array2. The example shows this.
Thats how it works on my php anyway.


karim dot u dot khan

The comment from "alex at bacan dot cl_remove_this_for_email" (the fifth from the bottom of the page as I write this) is wrong--maybe it worked around a bug in 2002 when it was written but it's not valid today. His array_minus_array() function does nothing different from array_diff. Gave me a heart attack for a second thinking my last project (which made heavy use of set operations) was flawed...

jappie

Sorry for the bug in my last comment (probably rightfully removed by the admins).
If you want to compare more than 2 arrays, or don't know how many arrays need to be compared, this is your function:
<?php
# An extention to array_diff:
# It returns an array of all values not present in all arrays given. If '$strict' is true,
# it returns all values not present or not in the same order in all arrays given. The
# arrays to compare must be placed in another array, which is used as argument '$arrays'.
# Returns false if the '$arrays' is invalid.
function array_rdiff ($arrays, $strict = false) {
   # check if argument is valid.
   if (!is_array ($arrays))
       return false;
   foreach ($arrays as $array)
       if (!is_array ($array))
           return false;
   # set working variables
   $diff    = array ();
   $amount  = count ($arrays);
   $needles = array_shift ($arrays);
   # compare
   for ($n = 0; $n < $amount; $n++) {
       for ($m = 0; $needles[$m]; $m++) {
           $found     = true;
           $positions = array ($m);
           foreach ($arrays as $haystack) {
               if (($pos = array_search ($needles[$m], $haystack)) === false)
                   $found = false;
               if ($strict)
                   $positions[] = $pos;
           }
           if (!$found)
               $diff[] = $needle;
           elseif ($strict && (count (array_unique ($positions)) > 1))
               $diff[] = $needle;
       }
       $arrays[] = $needles;
       $needles  = array_shift ($arrays);
   }
   return array_unique ($diff);
}
?>


dead dot screamer

radim's function <?array_obj_diff()?> clone objects in arrays in PHP5, better is use <?array_uDiff()?>:
<?php
...
function arrayObjDiff($array1, $array2)
{
    static $cmpFunction=create_Function('$a, $b',
       'return strCmp(serialize($a),serialize($b));');
  //this function has similar args as array_Diff()
   $args=func_Get_Args();
   $args[]=$cmpFunction;
   
   return call_User_Func_Array('array_uDiff',$args);
}
array_obj_diff($ao1,$ao2);
?>


ben dot lancaster

One common caveat of this function is that if the arrays match, an empty array is return, not a strict boolean. E.g.:
<?php
$array1 = $array2 = array('a','b','c');
var_dump(array_diff($array1,$array2));
/*
*Returns:
* array(0) {
* }
*/
?>


david

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.


mr dot kschan

just comment ... i don't know whether the following implementation of array_diff is having a much better meaning to the function name.
function ary_diff( $ary_1, $ary_2 ) {
 // compare the value of 2 array
 // get differences that in ary_1 but not in ary_2
 // get difference that in ary_2 but not in ary_1
 // return the unique difference between value of 2 array
 $diff = array();
 // get differences that in ary_1 but not in ary_2
 foreach ( $ary_1 as $v1 ) {
   $flag = 0;
   foreach ( $ary_2 as $v2 ) {
     $flag |= ( $v1 == $v2 );
     if ( $flag ) break;
   }
   if ( !$flag ) array_push( $diff, $v1 );
 }
 // get difference that in ary_2 but not in ary_1
 foreach ( $ary_2 as $v2 ) {
   $flag = 0;
   foreach ( $ary_1 as $v1 ) {
     $flag |= ( $v1 == $v2 );
     if ( $flag ) break;
   }
   if ( !$flag && !in_array( $v2, $diff ) ) array_push( $diff, $v2 );
 }
 return $diff;
}
i think array_diff should return the difference between the array independent of the order of passing the parameter.


penguin

Interesting effect...
I just wanted to see, what variables are created and not unset after some actions.
So I write:
<?php
$a_var_GlobalsBefore = $GLOBALS;
?>
...some actions with variables
<?php
$a_var_GlobalsAfter = $GLOBALS;
$a_var_VariablesNotUnset = array_diff ($a_var_GlobalsAfter, $a_var_GlobalsBefore);
?>
...and _ALL_ variables puff up in smoke.
Have no idea what causes this behavior, but found a nice workaround:
<?php
$a_var_GlobalsBefore = array_keys ($GLOBALS);
?>
...some actions with variables
<?php
$a_var_GlobalsAfter = array_keys ($GLOBALS);
$a_var_VariablesNotUnset = array_diff ($a_var_GlobalsAfter, $a_var_GlobalsBefore);
?>
...and the unset variables are here.


caugustin

In version 4.0.1 to 4.0.4, array_diff() works on array of array, but not anymore in version 4.0.5 and 4.0.6.
According to php team :
When I wrote array_diff I didn't think of this use. It
worked by accident. array_diff was changed to avoid
some ordering problems. The way it was there was no well
defined ordering. Due to automatic type conversion, you
you would have 3d < 99 <  370 < 3d. This made
array_diff fail, this was fixed by always using string
comparisons. That doesn't work for arrays though.


arjanz

In PHP 4.0.6 the array_minus_array() as shown above didn't work properly, especially when you use duplicated values:
When I wanted to substract
     'Array(1,1,2,3)'
with
     'Array (1,2,4)'
I wanted as result:
'Array(1,3)'
And not just 'Array (3)' as it would give using array_diff(), or nothing as result using the above array_minus_array function.
This worked for me:
<?php
function array_minus_array($a,$b)  {
$c = Array();
foreach ($a as $key => $val) {  
  $posb = array_search($val,$b);
  if (is_integer($posb)) {
  unset($b[$posb]);  
  } else {
  $c[] = $val;
  }
}
return $c;
}
?>


rudigier

if you don't like the php diff (like me) then take look at my diff functions
if you have two arrays, lets say:
a1 = { blue, red, green }
a2 = { blue, yellow, green }
array_diff just gives me nothing, although there is a difference. instead of an empty array, i needed an array which should contain "red" and "yellow" in this case.
single_diff satisfies my wish.
function single_diff(&$a1,&$a2)
{
 $r = array(); // return
 
 foreach ($a1 as $pl) // payload
 {
   if (! in_array($pl, $a2, true) )
     $r[] = $pl;
 }
 
 foreach ($a2 as $pl) // payload
 {
   if (! in_array($pl, $a1, true) && ! in_array($pl, $r, true) )
     $r[] = $pl;
 }
 return $r;
}
--------------------
this one just keeps the keys of the arrays in mind by comparing the values directly. just in case if there are values, which might occur multiple in one of these arrays.
function single_diff_assoc(&$a1,&$a2)
{
 $r = array(); // return
 
 foreach ($a1 as $k => $pl) // payload
 {
   if (! isset($a2[$k]) || $a2[$k] != $pl)
     $r[$k] = $pl;
 }
 
 foreach ($a2 as $k => $pl) // payload
 {
   if ( (! isset($a1[$k]) || $a1[$k] != $pl ) && ! isset($r[$k]) )
     $r[$k] = $pl;
 }
 return $r;
}
recursion support might be useful, but feel free to modify this functions to your own purposes.


d dot u dot phpnet

I was not satisfied with the compare-funtions of array_diff() so i wrote a litte diff-clone which compares arrays and shows all different entries. You can easliy change the function formatline() for your needs.
function arr_diff( $a1, $a2, $show_matches=0);
...
output:
1 : 1 : - <head><title>Text</title></head>
1 : 1 : + <head><title>Text2</title></head>
4 : 4 : - code b
4 : 4 : + code a
8 : 8 : - code f
See details on:  (example and source)
http://www.holomind.de/phpnet/diff.php
http://www.holomind.de/phpnet/diff.src.php


gilthans

I needed a function to only remove the element the amount of times he appears in the second array. In other words, if you have Array(1, 1, 2) and Array(1), the return value should be Array(1, 2).
So I built this function right here:
<?php
function array_diff_once(){
if(($args = func_num_args()) < 2)
return false;
$arr1 = func_get_arg(0);
$arr2 = func_get_arg(1);
if(!is_array($arr1) || !is_array($arr2))
return false;
foreach($arr2 as $remove){
foreach($arr1 as $k=>$v){
if((string)$v === (string)$remove){ //NOTE: if you need the diff to be STRICT, remove both the '(string)'s
unset($arr1[$k]);
break; //That's pretty much the only difference from the real array_diff :P
}
}
}
//Handle more than 2 arguments
$c = $args;
while($c > 2){
$c--;
$arr1 = array_diff_once($arr1, func_get_arg($args-$c+1));
}
return $arr1;
}
$arr1 = Array("blue", "four"=>4, "color"=>"red", "blue", "green", "green", "name"=>"jon", "green");
$arr2 = Array("4", "red", "blue", "green");
print_r(array_diff_once($arr1, $arr2));
?>
This prints:
Array ( [1] => blue [3] => green [name] => jon [4] => green )
Note that it removes the elements left to right, opposite to what you might expect; in my case the order of elements had no importance. Fixing that would require a small variation.


seanecoates

I just came upon a really good use for array_diff(). When reading a dir(opendir;readdir), I _rarely_ want "." or ".." to be in the array of files I'm creating. Here's a simple way to remove them:
<?php
$someFiles = array();
$dp = opendir("/some/dir");
while($someFiles[] = readdir($dp));
closedir($dp);

$removeDirs = array(".","..");
$someFiles = array_diff($someFiles, $removeDirs);

foreach($someFiles AS $thisFile) echo $thisFile."\n";
?>
S


jg

I have found an interesting use for array_diff. I remember seing somebody having trouble deleting from an array because it left "holes".
I thought about using array_diff to fix the problem. Try running this code and see what it does:
<?php
for($i=0;$i<4;$i++)
{
print("Deleting item $i from the array
");
$my_array = array("value1","value2","value3","value4");
$my_array = array_diff($my_array,array_slice($my_array,$i,1));
foreach($my_array as $value)print(" -->$value
\n");
}
?>
It seems to work for every member of the array. I'm not sure how efficient it would be on really large arrays, though.
JG Estiot


j dot j dot d dot mol

Here is some code to take the difference of two arrays. It allows custom modifications like prefixing with a certain string (as shown) or custom compare functions.
<?php
// returns all elements in $all which are not in $used in O(n log n) time.
 // elements from $all are prefixed with $prefix_all.
 // elements from $used are prefixed with $prefix_used.
 function filter_unused( $all, $used, $prefix_all = "", $prefix_used = "" ) {
     $unused = array();
     // prefixes are not needed for sorting
     sort( $all );
     sort( $used );
     $a = 0;
     $u = 0;
     $maxa = sizeof($all)-1;
     $maxu = sizeof($used)-1;
     while( true ) {
         if( $a > $maxa ) {
             // done; rest of $used isn't in $all
             break;
         }
         if( $u > $maxu ) {
             // rest of $all is unused
             for( ; $a <= $maxa; $a++ ) {
                 $unused[] = $all[$a];
             }
             break;
         }
         if( $prefix_all.$all[$a] > $prefix_used.$used[$u] ) {
             // $used[$u] isn't in $all?
             $u++;
             continue;
         }
         if( $prefix_all.$all[$a] == $prefix_used.$used[$u] ) {
             // $all[$a] is used
             $a++;
             $u++;
             continue;
         }
         $unused[] = $all[$a];
         $a++;
     }
     return $unused;
 }
?>


rafmav

Here is a few functions to do a fast diff between two arrays in a few lines.
You can use it with other functions described in the function array_merge : array_merge_replace from an other user, and two functions using it : array_merge_diff and array_merge_diff_reverse.
Note that the keys are preserved!
<?
// returns a two dimensions array with the deleted data
// and the added data
function array_diff_both($new,$old)
{
$del=array_diff_assoc($old,$new);
$add=array_diff_assoc($new,$old);
return $diff=array("del"=>$del, "add"=>$add);
}
// returns a two dimensions array with the equal data,
// deleted data and the added data
function array_diff_all($arr_new,$arr_old)
{
$arr_equ=array_intersect_assoc($arr_new,$arr_old);
$arr_del=array_diff_assoc($arr_old,$arr_new);
$arr_add=array_diff_assoc($arr_new,$arr_old);
return $diff=array("equ"=>$arr_equ, "del"=>$arr_del, "add"=>$arr_add);
}
?>


19-nov-2003 12:54

From the page:
Note:  Please note that this function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);
I've found a way to bypass that. I had 2 arrays made of arrays.
I wanted to extract from the first array all the arrays not found in the second array. So I used the serialize() function:
<?php
function my_serialize(&$arr,$pos){
 $arr = serialize($arr);
}
function my_unserialize(&$arr,$pos){
 $arr = unserialize($arr);
}
//make a copy
$first_array_s = $first_array;
$second_array_s = $second_array;
// serialize all sub-arrays
array_walk($first_array_s,'my_serialize');
array_walk($second_array_s,'my_serialize');
// array_diff the serialized versions
$diff = array_diff($first_array_s,$second_array_s);
// unserialize the result
array_walk($diff,'my_unserialize');
// you've got it!
print_r($diff);
?>


alex

For those who are looking how to substract one array for another:
you cannot use the "-" operator !!!
But you can use this function:
<?php
function array_minus_array($a, $b) {
       $c=array_diff($a,$b);
       $c=array_intersect($c, $a);
       return $c;
}
?>
So, for example:
<?php
$a[]="a";
$a[]="b";
$a[]="c";
$b[]="a";
$c=array_minus_array($a,$b);
var_dump($c);
?>
Prints out:
array(2) {
 [1]=>
 string(1) "b"
 [2]=>
 string(1) "c"
}
which is $a-$b, as we wanted.
PD: you may also do a foreach ... if $a in_array $b ... but this is much more elegant !!!
Credits to A.Atala "mandrake", who knows everything about mathematic.


asdf

Even tough the description of this function's behavior has changed it
does not appear as tough the function's behavior has changed.  Below is some test code and its output:
<?php
$a = array('1','2','3','4','5');
$b = array('1','a','b','c','d','e');
$c = array('2','f','g','h','i','j');

print_r(array_diff($a, $b, $c));
?>
Array
(
   [2] => 3
   [3] => 4
   [4] => 5
)


sire

Continuing from r.kirschke's excellent diff function (above), here's a function that will turn the result into nicely formatted HTML:
<?php
echo "<html><body bgcolor=white>";
$oldString = "Once there was a boy named Bart and a girl named Lisa.";
$newString = "Once upon a time there was a girl named Lisa.";
echo "Old String: " . $oldString . "
";
echo "New String: " . $newString . "
";
echo "Difference: " . diff_to_html($oldString, $newString);
// Returns a nicely formatted html string
function diff_to_html($oldString, $newString)
{
 $a1 = explode(" ", $oldString);
 $a2 = explode(" ", $newString);
 $result = arr_diff($a1, $a2);
 foreach ($result[0] as $num => $foo)
 {
   $source = $result[1][$num];
   $element = $result[0][$num];
   switch ($source)
   {
     case "1":
       $pre = "<font color=red><s>";
       $post = "</s></font>";
       break;
     case "2":
       $pre = "<font color=green>";
       $post = "</font>";
       break;
     case "b":
       $pre = "";
       $post = "";
       break;
   }
   // VERTICAL OUTPUT:
   // $return .= $num . $pre . " " . $source .
   // " " . $element . $post . "
";
   // READABLE OUTPUT:
   $return .= $pre . $element . $post . " ";
 }
 return $return;
}
?>


03-aug-2006 08:30

array_diff provides a handy way of deleting array elements by their value, without having to unset it by key, through a lengthy foreach loop and then having to rekey the array.
<?php
//pass value you wish to delete and the array to delete from
function array_delete( $value, $array)
{
   $array = array_diff( $array, array($value) );
   return $array;
}
?>


radim

array_diff doesn't work with arrays of objects because it compares the string-represantation of the arguments (which is always "Object" for an object).
Here is an easy way to compute the difference of two arrays of objects :
<?php
...
function array_obj_diff ($array1, $array2) {

foreach ($array1 as $key => $value) {
$array1[$key] = serialize ($value);
}
foreach ($array2 as $key => $value) {
$array2[$key] = serialize ($value);
}

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

foreach ($array_diff as $key => $value) {
$array_diff[$key] = unserialize ($value);
}

return $array_diff;
}
array_obj_diff ($ao1, $ao2);
?>


drnospamtdiggersspamno

array_diff does not have buggy behavior as described above.  The problem stems from calling array_diff() each time in the loop, therefore regererating a new array with an index always at the beginning EVERY time, so each will always pick the first entry.  This is not buggy, but in fact what you've told the program to do :)  The solution is not as much a solution, but properly instructing the program what to do!
Cheers!
TheoDiggers


r dot kirschke

Are you looking for a function which returns an edit script (a set of insert and delete instructions on how to change one array into another)? At least, that's what I hoped to find here, so here's some code based on http://www.cs.arizona.edu/people/gene/PAPERS/diff.ps :
<?php
function diff_rek(&$a1,&$a2,$D,$k,&$vbck)
{
$x=$vbck[$D][$k]; $y=$x-$k;
if ($D==0)
{
 if ($x==0) return array(array(),array());
 else
 return array(array_slice($a1,0,$x),array_fill(0,$x,"b"));
}
$x2=$vbck[$D-1][$k+1];
$y2=$vbck[$D-1][$k-1]-($k-1);
$xdif=$x-$x2; $ydif=$y-$y2;
$l=min($x-$x2,$y-$y2);
$x=$x-$l;
$y=$y-$l;
if ($x==$x2)
{
  $res=diff_rek($a1,$a2,$D-1,$k+1,$vbck);
  array_push($res[0],$a2[$y-1]);
  array_push($res[1],"2");
  if ($l>0)
  {
   $res[0]=array_merge($res[0],array_slice($a2,$y,$l));
   $res[1]=array_merge($res[1],array_fill(0,$l,"b"));
  }
}
else
{
  $res=diff_rek($a1,$a2,$D-1,$k-1,$vbck);
  array_push($res[0],$a1[$x-1]);
  array_push($res[1],"1");
  if ($l>0)
  {
   $res[0]=array_merge($res[0],array_slice($a1,$x,$l));
   $res[1]=array_merge($res[1],array_fill(0,$l,"b"));
  }
}
return $res;
}
function arr_diff(&$a1,&$a2)
{
$max=70;
$c1=count($a1);
$c2=count($a2);
$v[1]=0;
for ($D=0; $D<=$max; $D++)
{
  for ($k=-$D; $k<=$D; $k=$k+2)
 {
  if (($k==-$D) || ($k!=$D && $v[$k-1]<$v[$k+1]))
   $x=$v[$k+1];
  else
   $x=$v[$k-1]+1;
  $y=$x-$k;
  while (($x<$c1)&&($y<$c2)&&($a1[$x]==$a2[$y]))
   {
    $x++;
    $y++;
   }
   $v[$k]=$x;
   if (($x>=$c1)&&($y>=$c2))
  {
   $vbck[$D]=$v;
   return diff_rek($a1,$a2,$D,$c1-$c2,$vbck);
  };
 }
 $vbck[$D]=$v;
};
return -1;
}
?>
This works on arrays of all elements for which the operator "==" is defined.
arr_dif($a1,$a2) returns an array of two arrays:
$result[0] = array of elements from $a1 and $a2
$result[1] = array of chars - one for each element from $result[0]:
"1" : The corresponding element is from $a1
"2" : The corresponding element is from $a2
"b" : The correspondig element is from both source arrays
The function returns -1, when the number of different elements is greater than $max
Example:
$a1=array("hello","world");
$a2=array("good","bye","world");
=> arr_diff($a1,$a2) = array(array("hello","good","bye","world"), array("1","2","2","b"));


ahigerd

An earlier comment suggested using array_merge() to reindex the array. While this will work, array_values() is about 30-40% faster and accomplishes the same task.

nilsandre

Again, the function's description is misleading right now. I sought a function, which (mathematically) computes A - B, or, written differently, A \ B. Or, again in other words, suppose
A := {a1, ..., an} and B:= {a1, b1, ... , bm}
=> array_diff(A,B) = {a2, ..., an}
array_diff(A,B) returns all elements from A, which are not elements of B (= A without B).
You should include this in the documentation more precisely, I think.


doug

After spending half an hour scratching my head wondering why this function wasn't working I realised I had the arguments the wrong way round!
I needed to remove the contents of $array1 from $array2 so I tried:
<?php
$diff    = array_diff($members1, $members2);
?>
WRONG!! A quick swap around and things worked smoothly...
<?php
$diff    = array_diff($members2, $members1);
?>
Hope this saves someone a bit of bother


hayley watson

A wee idiom for KEEPING only those elements of an array $a that appear in an array $b (without introducing any elements of $b that do not appear in $a).
<?php
array_diff($a, array_diff($a, $b))
?>
And for mr dot kschan at gmail dot com: if it's the SYMMETRIC difference you want, (A-B typically is not equal to B-A; consider the case when A and B are integers), there is Viking Coder's comment 04-Jan-2006,
or there is the alternative:
<?php
function symmetric_array_diff($array1, $array2)
{
return array_diff(array_merge($array1, $array2), array_intersect($array1, $array2));
}
?>
Viking Coder's expression and mine are equivalent: (A-B)u(B-A) == (AuB)-(AnB). They're probably about equal in efficiency.


air

A small thing that caused me trouble today, wich I don't see listed on this page is that array_diff keeps the placing for the uniqe values, and removes the duplicated. This gives us empty fields in the array, wich caused me a lot of trouble. The solutions was simply to use array_merge() around the array_diff.
For example:
$array1 = array('blue', 'red', 'green');
$array2 = array('red');
array_diff($array1, $array2);
Will give us:
------
Array
(
  [0] => red
  [1] =>
  [2] => green
)
But if we use:
array_merge(array_diff($array1, $array2));
We will get:
------
Array
(
  [0] => red
  [1] => green
)


simon riget

A simple multidimentional key aware array_diff function.

<?php
function arr_diff($a1,$a2){
 foreach($a1 as $k=>$v){
   unset($dv);
   if(is_int($k)){
     // Compare values
     if(array_search($v,$a2)===false) $dv=$v;
     else if(is_array($v)) $dv=arr_diff($v,$a2[$k]);
     if($dv) $diff[]=$dv;
   }else{
     // Compare noninteger keys
     if(!$a2[$k]) $dv=$v;
     else if(is_array($v)) $dv=arr_diff($v,$a2[$k]);
     if($dv) $diff[$k]=$dv;
   }
 }
 return $diff;
}
?>
This function meets my immidiate needs but I'm shure it can be improved.


csaba2000

<?php
function array_key_diff($ar1, $ar2) {  // , $ar3, $ar4, ...
   // returns copy of array $ar1 with those entries removed
   // whose keys appear as keys in any of the other function args
   $aSubtrahends = array_slice(func_get_args(),1);
   foreach ($ar1 as $key => $val)
       foreach ($aSubtrahends as $aSubtrahend)
           if (array_key_exists($key, $aSubtrahend))
               unset ($ar1[$key]);
   return $ar1;
}
$a = array("c" => "catty", "b" => "batty", "a" => "aunty", 5 => 4, 2.9 => 7, 11, "n" => "nutty");
$b = array(9, "d" => "ditty", "b" => "bratty", "a" => null, 10, 13);
$c = array_key_diff ($a, $b, array(5 => 6));
?>
$c is then equivalent to array('c' => 'catty', 6 => 11, 'n' => 'nutty')
Csaba Gabor from New York


pavlicic

<?php
// first array
$vid_player = $vm->getVideosByPlayer($player);
// second array
$vid_playlist = $vm->getVideosByPlaylist($playlist);
// this will not work...
$vid_player = array_diff($vid_player, $vid_playlist);
// but if you do this first...
$videos = array();
foreach ( $vid_player as $player )
{
if ( $vid_playlist != null )
{
foreach ( $vid_playlist as $video )
{
if ( $player->id == $video->id )
$videos[] = $player;
}
}
}
// this will work...
$vid_player = array_diff($vid_player, $videos);
?>
The first array_diff() compares two arrays only to find out that all the objects are unique!


Change Language


Follow Navioo On Twitter
array_change_key_case
array_chunk
array_combine
array_count_values
array_diff_assoc
array_diff_key
array_diff_uassoc
array_diff_ukey
array_diff
array_fill_keys
array_fill
array_filter
array_flip
array_intersect_assoc
array_intersect_key
array_intersect_uassoc
array_intersect_ukey
array_intersect
array_key_exists
array_keys
array_map
array_merge_recursive
array_merge
array_multisort
array_pad
array_pop
array_product
array_push
array_rand
array_reduce
array_reverse
array_search
array_shift
array_slice
array_splice
array_sum
array_udiff_assoc
array_udiff_uassoc
array_udiff
array_uintersect_assoc
array_uintersect_uassoc
array_uintersect
array_unique
array_unshift
array_values
array_walk_recursive
array_walk
array
arsort
asort
compact
count
current
each
end
extract
in_array
key
krsort
ksort
list
natcasesort
natsort
next
pos
prev
range
reset
rsort
shuffle
sizeof
sort
uasort
uksort
usort
eXTReMe Tracker