How do I count occurrence of duplicate items in array

前端 未结 12 1303
我寻月下人不归
我寻月下人不归 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:27

    Count duplicate element of an array in PHP without using in-built function

    $arraychars=array("or","red","yellow","green","red","yellow","yellow");
    $arrCount=array();
            for($i=0;$i<$arrlength-1;$i++)
            {
              $key=$arraychars[$i];
              if($arrCount[$key]>=1)
                {
                  $arrCount[$key]++;
                } else{
                  $arrCount[$key]=1;
            }
            echo $arraychars[$i]."
    "; } echo "
    ";
            print_r($arrCount);
    

提交回复
热议问题