note 14715 modified in function.array-unique by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:50:45 +0000 |
| Subject: | note 14715 modified in function.array-unique by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-80039@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:
<?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;
}
?>
--was--
>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://php.net/manual/en/function.array-unique.php