note 14715 deleted from function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 17:26:20 +0000 |
| Subject: | note 14715 deleted from function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176204@lists.php.net to get a copy of this message | ||
Note Submitter: eltehaem at N dot O dot S dot P dot A dot M dot free dot poltronic dot net
----
>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:
<?php
$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:
<?php
function my_array_unique(&$old){
$new = array();
foreach($old as $key => $value){
if(!in_array($value, $new)) $new[$key] = $value;
}
return $new;
}
?>