How do I count occurrence of duplicate items in array

前端 未结 12 1260
我寻月下人不归
我寻月下人不归 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]."<br>";
         }
            echo "<pre>";
            print_r($arrCount);
    
    0 讨论(0)
  • 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
                )
    
    0 讨论(0)
  • 2020-11-27 04:35

    array_count_values, enjoy :-)

    $array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
    $vals = array_count_values($array);
    echo 'No. of NON Duplicate Items: '.count($vals).'<br><br>';
    print_r($vals);
    

    Result:

    No. of NON Duplicate Items: 7
    Array
    (
        [12] => 1
        [43] => 6
        [66] => 1
        [21] => 2
        [56] => 1
        [78] => 2
        [100] => 1
    )
    
    0 讨论(0)
  • 2020-11-27 04:35

    I actually wrote a function recently that would check for a substring within an array that will come in handy in this situation.

    function strInArray($haystack, $needle) {
        $i = 0;
        foreach ($haystack as $value) {
            $result = stripos($value,$needle);
            if ($result !== FALSE) return TRUE;
            $i++;
        }
        return FALSE;
    }
    
    $array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
    
    for ($i = 0; $i < count($array); $i++) {
        if (strInArray($array,$array[$i])) {
            unset($array[$i]);
        }
    }
    var_dump($array);
    
    0 讨论(0)
  • 2020-11-27 04:35

    If you have a multi-dimensional array you can use on PHP 5.5+ this:

    array_count_values(array_column($array, 'key'))
    

    which returns e.g.

     [
       'keyA' => 4,
       'keyB' => 2,
     ]
    
    0 讨论(0)
  • 2020-11-27 04:37

    You can also use it with text items array, u will get number of duplicates properly, but PHP shows

    Warning: array_count_values(): Can only count STRING and INTEGER values!

    $domains = 
    array (
      0 => 'i1.wp.com',
      1 => 'i1.wp.com',
      2 => 'i2.wp.com',
      3 => 'i0.wp.com',
      4 => 'i2.wp.com',
      5 => 'i2.wp.com',
      6 => 'i0.wp.com',
      7 => 'i2.wp.com',
      8 => 'i0.wp.com',
      9 => 'i0.wp.com' );
    
    $tmp = array_count_values($domains);
    print_r ($tmp);
    
        array (
          'i1.wp.com' => 2730,
          'i2.wp.com' => 2861,
          'i0.wp.com' => 2807
        )
    
    0 讨论(0)
提交回复
热议问题