php 统计一维数组中重复的元素个数

<?php
echo "<pre>";
$array = array(1, 1, 1, 54, 3,4, 3,4, 3, 14, 3,4, 3,7,8,9,12,45,66,5,7,8,9,2,45);

$a =0;
foreach ($array as $str) {
    @$result[$str] = $result[$str] + 1;
    $a = $a + 1;
}
echo $a;
print_r($result);
25Array
(
    [1] => 3
    [54] => 1
    [3] => 5
    [4] => 3
    [14] => 1
    [7] => 2
    [8] => 2
    [9] => 2
    [12] => 1
    [45] => 2
    [66] => 1
    [5] => 1
    [2] => 1
)

 

<?php
echo "<pre>";
$array = array(1, 1, 1, 54, 3,4, 3,4, 3, 14, 3,4, 3,7,8,9,12,45,66,5,7,8,9,2,45);


foreach ($array as $str) {
    
    @$result[$str] = $result[$str] + 1;
    //$result[1] = 0 + 1;
    //$result[1] = 1 + 1;
    //$result[1] = 2 + 1;
    //$result[54] = 0 + 1;
    //$result[3] = 0 + 1;

}

print_r($result);

 

posted @ 2017-03-20 14:48  fleam  阅读(4586)  评论(0编辑  收藏  举报