note 14715 added to function.array-unique.php
| From: | eltehaem at N dot O dot S dot P dot A dot M dot free dot poltronic dot net | Date: | Sat, 11 Aug 2001 11:26:34 +0000 |
| Subject: | note 14715 added to function.array-unique.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-16576@lists.php.net to get a copy of this message | ||
>array_unique() will keep the first key encountered
>for every value, and ignore all following keys.
This is true only in PHP 4.0.4pl1, i.e. the code below:
$temp = Array ('pl', 'pl', 'en', 'en', 'cz',
'de', 'en', 'pl');
print_r($temp);
print_r(array_unique($temp));
Will produce this output:
Array
(
[0] => pl
[1] => pl
[2] => en
[3] => en
[4] => cz
[5] => de
[6] => en
[7] => pl
)
Array
(
[0] => pl
[2] => en
[4] => cz
[5] => de
)
And in PHP 4.0.3pl1 will produce this output:
Array
(
[0] => pl
[1] => pl
[2] => en
[3] => en
[4] => cz
[5] => de
[6] => en
[7] => pl
)
Array
(
[4] => cz
[5] => de
[6] => en
[7] => pl
)
This little function resolves the problem:
function my_array_unique(&$old){
$new = array();
foreach($old as $key => $value){
if(!in_array($value, $new)) $new[$key] = $value;
}
return $new;
}
http://www.php.net/manual/en/function.array-unique.php