but only when the two arrays contain the same number of values and then only in some cases. Otherwise the latter method will be radically faster due to the use of a count() test before the array_diff().

Also, if the two arrays contain a different number of values, then which method is faster will depend on whether both arrays need to be sorted or not. Two times sort() is a bit slower than one time array_diff(), but if one of the arrays have already been sorted, then you only have to sort the other array and this will be almost twice as fast as array_diff().

# loop through the validated array and move elements to $array1 # this is necessary because the array_diff function returns arrays that retain their original keysforeach ($valid_array as $valid){$array1[] = $valid; } return $array1; }?>

Hi!I tried hard to find a solution to a problem I'm going to explain here, and after have read all the array functions and possibilities, I had to create what I think should exist on next PHP releases.

What I needed, it's some kind of Difference, but working with two arrays and modifying them at time, not returning an array as a result with the diference itself.

So, as an example:

A = 1,2,3B = 2,3,4

should NOT be:

C = 1,4

but:

A = 1B = 4

so basically, I wanted to delete coincidences on both arrays.

Now, I've some actions to do, and I know wich one I've to do with the values from one array or another.With the normal DIFF I can't, because if I've an array like C=1,4, I dont know if I've to do the Action_A with 1 or with 4, but I really know that everything in A, will go to the Action_A and everithing in B, will go to Action_B. So same happens with 4, don't know wich action to apply...

This cute, works by reference, and modifies the arrays deleting coincidences on both, and leaving intact the non coincidences.

So a call to this will be somethin' like:

<?php$original = array(1,2,3);$new = array(2,3,4);

array_diff_ORG_NEW($original, $new, 'VALUES');?>

And HERE, I'll have my arrays as I wanted:

$original = 1$new = 4

Now, why I use it precisely?

Imagine you've some "Events" and some users you select when create the event, can "see" this event you create. So you "share" the event with some users. Ok?

Imagine you created and Event_A, and shared with users 1,2,3.

Now you want to modify the event, and you decide to modify the users to share it. Imagine you change it to users 2,3,4.

(numbers are users ID).

So you can manage when you are going to modify, to have an array with the IDs in DDBB ($original), and then, have another array with ID's corresponding to the users to share after modifying ($new). Wich ones you've to DELETE from DDBB, and wich ones do you've to INSERT?

If you do a simple difference or somehow, you get somethin' like C=1,4.You have no clue on wich one you've to insert or delete.

But on this way, you can know it, and that's why:

- What keeps on $original, it's somethin not existing in $new at the beggining. So you know that all what you've inside $original, have to be deleted from DDBB because what you did in the modifying process, it's to unselect those users keeping in $original.- What keeps on $new, it's something not existing in $original at the beggining. Wich means that in the modifying process you added some new users. And those have to be inserted in DDBB. So, everything keeping inside $new, have to be inserted in the DDBB.

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();

I´ve been looking for a array_diff that works with recursive arrays, I´ve tried the ottodenn at gmail dot com function but to my case it doesn´t worked as expected, so I made my own. I´ve haven´t tested this extensively, but I´ll explain my scenario, and this works great at that case :D

Ok, now some comments about this function:
- Different from the PHP array_diff, this function DON´T uses the === operator, but the ==, so 0 is equal to '0' or false, but this can be changed with no impacts.
- This function checks the keys of the arrays, array_diff only compares the values.

I realy hopes that this could help some1 as I´ve been helped a lot with some users experiences. (Just please double check if it would work for your case, as I sad I just tested to a scenario like the one I exposed)

/** * array_diffs — Computes the difference of all the arrays * * @param array * * array1 - The array to compare from and against * array2 - The array to compare from and against * array(n) - More arrays to compare from and against * * @return array Returns all the arrays that do contains entries that cannot be matched in any of the arrays. */

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 datafunction array_diff_both($new,$old){ $del=array_diff_assoc($old,$new); $add=array_diff_assoc($new,$old); return $diff=array("del"=>$del, "add"=>$add);}

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:

Hi, I´d like to give a piece of advice to all who need to use this function to compare two arrays that have a great quantity of elements. You should sort both arrays first before comparing, it will work faster.Thanks

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;

As touched on in kitchin's comment of 19-Jun-2007 03:49 and nilsandre at gmx dot de's comment of 17-Jul-2007 10:45, array_diff's behavior may be counter-intuitive if you aren't thinking in terms of set theory.

array_diff() returns a *mathematical* difference (a.k.a. subtraction) of elements in array A that are in array B and *not* what elements are different between the arrays (i.e. those that elements that are in either A or B but aren't in both A and B).

Drawing one of those Ven diagrams or Euler diagrams may help with visualization...

As far as a function for returning what you may be expecting, here's one:

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.

Resubmitting... the update for takes into account comparison issues Computes the difference of all the arrays<?php

/** * array_diffs â€” Computes the difference of all the arrays * * @param array * * array1 - The array to compare from and against * array2 - The array to compare from and against * array(n) - More arrays to compare from and against * * @return array Returns all the arrays that do contains entries that cannot be matched in any of the arrays. */

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!

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 :

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