Wez Furlong wrote:
Should these functions really go into the core?
Perhaps they are better off in an extension (pecl/array_utils ?)The names aren't really all that obvious to me either.
--Wez.
Hi Wez,
well maybe array_udiff_uassoc() and array_diff_uassoc() are of little
need but I added them for completeness to array_udiff() and
array_udiff_assoc(). It's easy not to export array_udiff_uassoc() and
array_diff_uassoc() and this won't hurt me. However I think that
array_udiff() and array_udiff_assoc() are useful. As I stated in my talk
to Jani, in 4_3 it is impossible to use array_diff() or
array_diff_assoc() on arrays that contain objects. When I started to
implement (and implemented) these functions few months ago I had no need
of them, I did it for fun. However, month or two ago I faced a case
where I needed this functionality but unfortunately it wasn't available
so I had to find a way to workaround it.
Let me show you a small example :
<?php
class CPerson {
private $priv_member;
public $public_member;
function CPerson($val) {
$this->priv_member = $val;
$this->public_member = $val;
}
static function comp_func_cr($a, $b) {
if ($a->priv_member === $b->priv_member) return 0;
return ($a->priv_member > $b->priv_member)? 1:-1;
}
}// class CPerson
$a = array(
"0.1" => new cr(9),
"0.5" => new cr(12),
0 => new cr(23),
1=> new cr(4),
2 => new cr(-15),
);
$b = array("0.2" => new cr(9),
"0.5" => new cr(22),
0 => new cr( 3),
1=> new cr(4),
2 => new cr(-15),
);
?>
In this case $a and $b contain objects. Even more they have private
member variables. array_diff() is in no use here since it cannot work
with objects well : if I execute array_diff($a, $b) the returned array
will contain the elements of $a . So, what to do to make a diff of the
two arrays and not to code a function (in PHP) for that by myself? ->
Implement core function for that. This functionality goes to 5.0.0 where
the object model is better and if core functions can work well with
object(s) it will be good and not bad IMO.
Using array_udiff() it is easy to diff $a and $b :
<?php
array_udiff($a, $b, array("cr", "comp_func_cr"));
?>
If there are better names for these functions I have nothing against to
rename them. I named them this way because of the function usort() that
sorts an array by using a callback function.
Regards,
Andrey
P.S.
I will appreciate the opinion of Marcus Boerger and Sebastian Bergmann
whether these functions are useful or not. However, any other opinions
are welcome (this is why i cross-post to internals@)
----- Original Message -----
From: "Andrey Hristov" andrey@php.net
To: php-cvs@lists.php.net
Sent: Tuesday, September 23, 2003 6:37 PM
Subject: [PHP-CVS] cvs: php-src /ext/standard array.c basic_functions.c
php_array.handrey Tue Sep 23 13:37:29 2003 EDT
Modified files:
/php-src/ext/standard array.c basic_functions.c php_array.h
Log:
4 new functions :
array_udiff()
array_udiff_assoc()
array_diff_uassoc()
array_udiff_uassoc()
They work likearray_diff()orarray_diff_assoc()but callbackfunction(s)
can be used to perform the comparisons. For example
array_udiff_uassoc()
expects 2 callbacks are last 2 parameters one is used to compare thevalues
of the entries in the arrays the second to compare the keys.
Class methods are also valid callbacks. Even when the data that shouldbe
used in the comparison is private or protected then a static method
of a
class should be used (this behaviour can be seen in the regressiontest -
007.phpt).