使用array_count_values
是要走的最简单的方法,但如果你需要掌握如何完成你在找什么,这里的详细的版本。
$input = array(1, 2, 3, 4, 1);
$unique = array_unique($input);
// If $input and $unique are different in length,
// there is one or more repeating values
if (count($input) !== count($unique)) {
$repeat = array();
// Sort values in order to have equal values next to each other
sort($input);
for ($i = 1; $i < count($input) - 1; $i++) {
// If two adjacent numbers are equal, that's a repeating number.
// Add that to the pile of repeated input, disregarding (at this stage)
// whether it is there already for simplicity.
if ($input[$i] === $input[$i - 1]) {
$repeat[] = $input[$i];
}
}
// Finally filter out any duplicates from the repeated values
$repeat = array_unique($repeat);
echo implode(', ', $repeat);
} else {
// All unique, display all
echo implode(', ', $input);
}
简明的单行十岁上下的版本是:
$input = array(1, 2, 3, 4, 1);
$repeat = array_keys(
array_filter(
array_count_values($input),
function ($freq) { return $freq > 1; }
)
);
echo count($repeat) > 0
? implode(', ', $repeat)
: implode(', ', $input);
hoppa我有我的问题是错误的,我从URL意味着代替URL,对不起,你可以帮我一个码?请你帮我,这非常非常重要请 – UGD 2011-12-29 14:55:56
好吧,至少你需要提供一个示例URL。 – hoppa 2011-12-29 15:04:19
这是整个代码*我修改了它)...但没有工作... – UGD 2011-12-29 15:10:50