note 53942 added to function.array-unique
| From: | ChristophZiegenberg at osu1 dot php dot net | Date: | Fri, 17 Jun 2005 18:44:12 +0000 |
| Subject: | note 53942 added to function.array-unique | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-90597@lists.php.net to get a copy of this message | ||
If the array key is a string it might be important to keep it although the value is the same (as I
need it at the moment). So I wrote a function which also returns array elements which have the same
value but different string keys.
function array_unique_numeric ($arr)
{
$str = $int = array();
foreach(array_keys($arr) as $key) {
${(is_int($key)?'int':'str')}[$key] = $arr[$key];
}
return array_merge($str, array_unique($int));
}
// typical array after an array_merge()...
$array = array("a" => "green", "b" => "brown",
"c" => "blue", "red", "d" => "green",
"yellow", "red");
print_r(array_unique($array));
// Array
// (
// [a] => green
// [b] => brown
// [c] => blue
// [0] => red
// [1] => yellow
// )
print_r(array_unique_numeric($array));
// Array
// (
// [a] => green
// [b] => brown
// [c] => blue
// [d] => green
// [0] => red
// [1] => yellow
// )
----
Manual Page -- http://www.php.net/manual/en/function.array-unique.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53942
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53942&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53942&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53942&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53942&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+53942&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53942&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53942&report=yes
Search -- http://master.php.net/manage/user-notes.php