Hallo,
array_unique() kann erst ab 5.2.9 mit multidimensionalen Arrays umgehen:
PHP: array_unique - Manual
Sprich array(array(1,2), array(1,3), array(1, 2)) liefert mir vor 5.2.9 ein array(array(1,2)) da Arrays als Strings behandelt "Array" geben, somit nur das erste Element behalten wird.
Nun brauche ich die Funktionalität aber in 5.2.5 (unserer Live-Umgebung). Update ausgeschlossen. Jetzt versuche ich die Funktion nachzubauen:
Bei Unit-Tests habe ich jetzt folgendes gefunden, was doch ein Bug ist oder?
PHP 5.2.9:
Ausgabe:
Das TRUE wird von array_unique() verschluckt, von meiner Implementierung nicht, daher bin ich drübergestolpert. FALSE, NULL, 1, 0 etc werden nicht verschluckt. Bug?
Könnt ihr das mal mit euren PHP-Versionen testen, oder ist mein Beispiel Schwachsinn (sehe gerade nurnoch Arrays
)
PS: md5((string)$variable) ist ungenau, sprich array_unique(array(null, "")) gibt (meiner Meinung nach unsinnigerweise) array(null) zurück. md5(serialize(array(gettype($variable), (string)$variable))) wäre allerdings nicht mehr identisch mit dem Handling von array_unique().
array_unique() kann erst ab 5.2.9 mit multidimensionalen Arrays umgehen:
PHP: array_unique - Manual
Note that keys are preserved. array_unique() sorts the values treated as string
[..]
5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.
[..]
5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.
Nun brauche ich die Funktionalität aber in 5.2.5 (unserer Live-Umgebung). Update ausgeschlossen. Jetzt versuche ich die Funktion nachzubauen:
PHP-Code:
<?php
class AIS_Util_Array
{
/**
* @link http://php.net/array_unique
* @desc 5.2.9: Added the optional sort_flags defaulting to SORT_REGULAR.
* Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.
*
* @param array $array
* @return array
*/
public static function unique(array $array)
{
if (version_compare(PHP_VERSION, "5.2.9") >= 0) {
return array_unique($array);
}
$isSimpleArray = empty($array) || (0 == count(
array_filter(
$array,
create_function(
'$e',
'return is_array($e) || is_object($e);'
)
)));
if ($isSimpleArray) {
// no array or object inside
return array_unique($array);
}
return self::_unique($array);
}
/* @desc allow access to own array_unique implementation no matter which php version */
public static function testUnique(array $array)
{
return self::_unique($array);
}
protected static function _unique(array $array)
{
$newArray = array();
foreach ($array as $key => $value) {
$newArray[$key] = self::_getHash($value);
}
$newArray = array_unique($newArray);
foreach ($newArray as $key => $value) {
$newArray[$key] = $array[$key];
}
return $newArray;
}
protected static function _getHash($variable)
{
if (is_array($variable)) {
return md5(
serialize(
array_combine(
array_keys($variable),
array_map(array(__CLASS__, "_getHash"), $variable)
)
)
);
} elseif (is_object($variable)) {
return md5(spl_object_hash($variable));
} else {
return md5((string)$variable);
}
}
}
PHP 5.2.9:
PHP-Code:
<?php
$array1 = array(array(1, 2, 3), true);
var_dump(AIS_Util_Array::testUnique($array1));
echo "<hr />";
var_dump(AIS_Util_Array::unique($array1));
echo "<hr />";
var_dump(array_unique($array1));
?>
Code:
array(2) { [0]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } [1]=> bool(true) } array(1) { [0]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } } array(1) { [0]=> array(3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) } }
Könnt ihr das mal mit euren PHP-Versionen testen, oder ist mein Beispiel Schwachsinn (sehe gerade nurnoch Arrays

PS: md5((string)$variable) ist ungenau, sprich array_unique(array(null, "")) gibt (meiner Meinung nach unsinnigerweise) array(null) zurück. md5(serialize(array(gettype($variable), (string)$variable))) wäre allerdings nicht mehr identisch mit dem Handling von array_unique().
Kommentar