0
$myArray = array(2, 7, 4, 2, 5, 7, 6, 7);

$uniques = array_unique($myArray);

除了仅在数组中显示每个值一次外,我还将如何显示(在下面的 foreach 循环中)每个值在数组中填充的次数。IE旁边'7'(数组值),我需要显示'3'(7在数组中的次数)

foreach ($uniques as $values) {
echo $values . " " /* need to display number of instances of this value right here */ ;
}
4

2 回答 2

0

请改用该array_count_values功能。

$myArray = array(2, 7, 4, 2, 5, 7, 6, 7);
$values = array_count_values($myArray);
foreach($values as $value => $count){
echo "$value ($count)<br/>";
}
于 2014-03-19T01:16:38.697 回答
0

看看array_count_values

引用手册:

Example #1 array_count_values() 例子

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

上面的示例将输出:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)
于 2014-03-19T01:16:45.567 回答