[PHP-DOC] Bug #12367 Updated: array_diff don't work with array of array (it did with 4.0.4) From: sniper <email protected>
Date: 07/27/01

ID: 12367
Updated by: sniper
Reported By: caugustin <email protected>
Old Status: Open
Status: Analyzed
Old Bug Type: Arrays related
Bug Type: Documentation problem
Operating System: Linux Red hat 7.1 fr
PHP Version: 4.0.6
New Comment:

Reclassified as docu prob.

Previous Comments:
------------------------------------------------------------------------

[2001-07-26 03:35:42] caugustin <email protected>

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-25 16:31:30] venaas <email protected>

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-25 11:36:08] caugustin <email protected>

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)

------------------------------------------------------------------------

Edit this bug report at http://bugs.php.net/?id=12367&edit=1