note 28892 modified in function.array-unique by vrana
| From: | vrana@php.net | Date: | Tue, 17 Aug 2004 14:50:28 +0000 |
| Subject: | note 28892 modified in function.array-unique by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-79976@lists.php.net to get a copy of this message | ||
To get a list of the duplicated values in an array, array_unique isn't much help. Instead, use
array_filter in conjunction with a callback function, as below:
<?php
$checkKeysUniqueComparison = create_function('$value','if ($value > 1) return
true;');
$result = array_keys (array_filter (array_count_values($array), $checkKeysUniqueComparison));
?>
These two lines therefore will create $result, an array of duplicated values in the array $array,
once each. E.g. the array
$array = array ("a", "b", "a", "b", "x",
"y", "z", "x");
gives the result
Array([0] => a [1] => b [2] => x)
--was--
To get a list of the duplicated values in an array, array_unique isn't much help. Instead, use
array_filter in conjunction with a callback function, as below:
$checkKeysUniqueComparison = create_function('$value','if ($value > 1) return
true;');
$result = array_keys (array_filter (array_count_values($array), $checkKeysUniqueComparison));
These two lines therefore will create $result, an array of duplicated values in the array $array,
once each. E.g. the array
$array = array ("a", "b", "a", "b", "x",
"y", "z", "x");
gives the result
Array([0] => a [1] => b [2] => x)
http://php.net/manual/en/function.array-unique.php