php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #12367 array_diff don't work with array of array (it did with 4.0.4)
Submitted: 2001-07-25 11:36 UTC Modified: 2001-09-14 19:15 UTC
From: caugustin at alcyonis dot fr Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.6 OS: Linux Red hat 7.1 fr
Private report: No CVE-ID: None
 [2001-07-25 11:36 UTC] caugustin at alcyonis dot fr
Example of script
<?
echo "<html><body>\r\n";
echo "<h1>Simple array : it works</h1>\r\n<pre>\r\n";
$array1 = array('a1' => 'toto', 'b1' => 'titi', 'b2' => 'tutu', 'b3' => 'rien', 'c1' => 'zone');
$array2 = array('a2' => 'adeux', 'b5' => 'titi');
var_dump(array_diff($array1, $array2));

echo "\r\n</pre>\r\n<hr>\r\n<h1>Array of array : it doesn't work</h1>\r\n<pre>\r\n";
$array1 = array(
   array('a1' => 'toto', 'b1' => 'titi', 'b2' => 'tutu', 'b3' => 'rien', 'c1' => 'zone'),
   array('a2' => 'adeux', 'b5' => 'titi'), 
   array('a8' => 'ahuit', 'b12' => 'avion')
);
$array2 = array(
   array('a2' => 'adeux', 'b5' => 'titi'),
   array('a7' => 'asept', 'c5' => 'camion')
);
var_dump(array_diff($array1, $array2));
echo "\r\n</pre></body></html>\r\n";
?>



Command line
 './configure' '--with-apache=../apache' '--with-zlib' '--with-bzip2' '--enable-calendar' '--with-cybermut=../cybermut' '--enable-ftp' '--with-gd=../gd' '--with-pgsql=/usr/local/psql' '--with-mysql' '--with-sablot' '--with-xml'


Comment
I use array_diff with array of array comming from database since version 4.0.1 without any trouble. We just migrate to version 4.0.6 tree days ago with apache 1.3.20.

Congratulation for PHP. I love it :o)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-25 16:31 UTC] venaas@php.net
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.

I'm not sure how you would like array_diff on arrays
of arrays work, and I don't plan to fix it. Maybe some-
one wants to, but first of all it must be clear how it
should work.

Could a work around for you be to serialize the arrays?
If $a is an array of arrays, you could perhaps do say
$a[0] = serialize($a[0]); $a[1] = serialize($a[1])...
and so on. One internal PHP solution could perhaps be
to serialize an array when comparing with scalars. Not
sure if I like it myself.

 [2001-07-26 03:35 UTC] caugustin at alcyonis dot fr
I don't understand why array_diff have to care about ordering. Php have enough sort function for array. But anyway, the solution of my problem is to work with sql and not with Php function.

Exemple of my use of array_diff :
* get all reccord of some properties from the database in an array of array (it could be an array of object).
* get a user properties the same way.
* make the difference between the both array to have all avaible properties for the given user.

So, the most important is to avoid loop of loop in php for such structure.

If you don't plan to fixe the way array_diff works, it should clarely explain in the documentation.
This function did simplifie my life with very complexe sql request.

Thanks for your quick answere. For me, you can close this bug.

Cedric.
 [2001-07-27 21:44 UTC] sniper@php.net
Reclassified as docu prob.



 [2001-09-14 19:15 UTC] jeroen@php.net
This doesn't need to be mentioned in the manual IMHO, since it was undocumented behaviour, and actually wasn't meant to work like this.
You shouldn't rely on undocumented behaviour anyway.

Note to caugustin: array_diff needs an ordering internally, otherwise it can't operate efficiently. 

Closed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 17:01:29 2024 UTC