note 24818 modified in function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Thu, 06 Nov 2008 15:10:25 +0000 |
| Subject: | note 24818 modified in function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146236@lists.php.net to get a copy of this message | ||
<?php
$truc = array("l810u00","l810u00","l810q00");
$machin = array_unique($truc);
for($i=0;$i < count($machin) ; $i++){
print $machin[$i]."
";
}
?>
result :
l810u00
This is not strange: $machin (as returned by array unique), contains "l810u00" either in
key[0] or key[1] but not both (the key depends on the ersion of PHP), and "l810q00" in
key[2].
The returned array has TWO elements so count($machin)==2.
The returned array has a hole in it, and you're not displaying its full content. You could
verify it by using this display loop instead:
foreach($machine as $key=>$value){
print '[' . $key . '] => ' . $value . '
";
}
result:
[0] => l810q00
[2] => l810u00
(the first line may display [1] instead of [0] for PHP 4.0.1p3, but you'll get the same order
of values and two lines, as expected). When calling array_values() on the result, you're
building a new array with the same values in the same order, but with renumbered keys (without holes
in numeric keys).
--was--
<?
$truc = array("l810u00","l810u00","l810q00");
$machin = array_unique($truc);
for($i=0;$i < count($machin) ; $i++){
print $machin[$i]."
";
}
?>
result :
l810u00
This is not strange: $machin (as returned by array unique), contains "l810u00" either in
key[0] or key[1] but not both (the key depends on the ersion of PHP), and "l810q00" in
key[2].
The returned array has TWO elements so count($machin)==2.
The returned array has a hole in it, and you're not displaying its full content. You could
verify it by using this display loop instead:
foreach($machine as $key=>$value){
print '[' . $key . '] => ' . $value . '
";
}
result:
[0] => l810q00
[2] => l810u00
(the first line may display [1] instead of [0] for PHP 4.0.1p3, but you'll get the same order
of values and two lines, as expected). When calling array_values() on the result, you're
building a new array with the same values in the same order, but with renumbered keys (without holes
in numeric keys).
http://php.net/manual/en/function.array-unique.php