How do I count occurrence of duplicate items in array

前端 未结 12 1329
我寻月下人不归
我寻月下人不归 2020-11-27 03:32

I would like to count the occurrence of each duplicate item in an array and end up with an array of only unique/non duplicate items with their respective occurrences.

<
12条回答
  •  误落风尘
    2020-11-27 04:31

    You can do it using foreach loop.

              $arrayVal = array(1,2,3,1,2,3,1,2,3,4,4,5,6,4,5,6,88);
              $set_array = array();
              foreach ($array as $value) {
                $set_array[$value]++;
              }
               print_r($set_array);
    

    Output :-

      Array( [1] => 3
                 [2] => 3
                 [3] => 3
                 [4] => 3
                 [5] => 2
                 [6] => 2
                 [88] => 1
                )
    

提交回复
热议问题