Sunday 15 September 2013

Creating a decently working version of array_diff in PHP

Creating a decently working version of array_diff in PHP

I have found that the array_diff function has several issues.
For example, array_diff(array(false), array(null)) returns an empty array,
I guess due to string conversion of the provided array's items. However,
null is a totally different value from false, so I conclude array_diff is
broken. Another problem array_diff suffers from appears when it is
presented with multidimensional arrays as arguments: the function will
start throwing notices all over the place.
Now what I desire is the exact same function, although working 100%. It
should do what array_diff is supposed to do, plus accepting
multidimensional arrays and strict comparing.
I have tried some things with array_udiff, but I don't really know what I
am doing, since hardly any explanation can be found on the documentation
page.
I thought it should be rather trivial to have a function that throws out
any items that don't occur in the first array passed to that function
(which is what array_diff supposedly does), but I can't seem to find an
efficient solution.

No comments:

Post a Comment