note 71718 deleted from function.array-unique by danbrown
| From: | danbrown@php.net | Date: | Sun, 23 Jan 2011 17:22:58 +0000 |
| Subject: | note 71718 deleted from function.array-unique by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176194@lists.php.net to get a copy of this message | ||
Note Submitter: n@cho NOSPAM cofrepuntonet
----
Note that array_unique preserves the keys, even if the array is not associative:
$values = Array("john@hotmail.com", "peter@hotmail.com",
"will@hotmail.com", "john@hotmail.com", "laura@hotmail.com",
"mariah@hotmail.com");
echo "<br>".count($values)." values.<br>";
var_dump($values);
$unique_values = array_unique($values);
echo "<br>".count($unique_values)." unique values.<br>";
var_dump($unique_values);
RESULT:
6 values.
array(6) {
[0]=>
string(16) "john@hotmail.com"
[1]=>
string(17) "peter@hotmail.com"
[2]=>
string(16) "will@hotmail.com"
[3]=>
string(16) "john@hotmail.com"
[4]=>
string(17) "laura@hotmail.com"
[5]=>
string(18) "mariah@hotmail.com"
}
5 unique values.
array(5) {
[0]=>
string(16) "john@hotmail.com"
[1]=>
string(17) "peter@hotmail.com"
[2]=>
string(16) "will@hotmail.com"
[4]=>
string(17) "laura@hotmail.com"
[5]=>
string(18) "mariah@hotmail.com"
}
$unique_values[3] is missing. Use array_merge or array_values if you need the index to be linear.