NYCPHP Meetup

NYPHP.org

[nycphp-talk] what's up with array_diff()

David Krings ramons at gmx.net
Wed Sep 19 18:47:18 EDT 2007


David Mintz wrote:
> Now I am looking at
> 
>        if (array_diff($one,$two) or array_diff($two,$one)) {
>            /* do something */
>        }
> 
> but I wonder if anyone can suggest a better way.
> 

I can only think of a worse way by looping through one array and seeing 
if the key/value pair exists in the other one. That might be OK for 
small arrays, but my assumption is that array_diff is way more 
optimized. I just wonder how if() will evaluate an array. Also, when the 
arrays are identical the return value is an empty array. Again, I am not 
sure if if() evaluates and empty array differently than an array with 
stuff in it. Also, comments on php.net indicate that when one array in 
fact is not array the return value of array_diff is NULL.
So, you probably need to run array_diff and then check the count of the 
resulting array. If it is anything else other than 0 the arrays are 
different. In theory, haven't tried that out.

Good luck!

David K.



More information about the talk mailing list