How do I count occurrence of duplicate items in array

前端 未结 12 1259
我寻月下人不归
我寻月下人不归 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:12
        $input = [1,2,1,3,2,4,10];
        //if give string
        //$input = "hello hello how are you how hello";
        //$array = explode(' ',$input);
        $count_val = [];
        foreach($array as $val){
          $count_val[$val]++;
        }
        print_r($count_val);
    //output ( [1] => 2 [2] => 2 [3] => 1 [4] => 1 [10] => 1 )
    
    0 讨论(0)
  • 2020-11-27 04:15

    There is a magical function PHP is offering to you it called in_array().

    Using parts of your code we will modify the loop as follows:

    <?php
    $array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
    $arr2 = array();
    $counter = 0;
    for($arr = 0; $arr < count($array); $arr++){
        if (in_array($array[$arr], $arr2)) {
            ++$counter;
            continue;
        }
        else{
            $arr2[] = $array[$arr];
        }
    }
    echo 'number of duplicates: '.$counter;
    print_r($arr2);
    ?>
    

    The above code snippet will return the number total number of repeated items i.e. form the sample array 43 is repeated 5 times, 78 is repeated 1 time and 21 is repeated 1 time, then it returns an array without repeat.

    0 讨论(0)
  • $search_string = 4;
    $original_array = [1,2,1,3,2,4,4,4,4,4,10];
    $step1 = implode(",", $original_array); // convert original_array to string
    $step2 = explode($search_string, $step1); // break step1 string into a new array using the search string as delimiter
    $result = count($step2)-1; // count the number of elements in the resulting array, minus the first empty element
    print_r($result); // result is 5
    
    0 讨论(0)
  • 2020-11-27 04:19

    if you want to try without 'array_count_values' you can do with a smart way here

    <?php
    $input= array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
    
    $count_values = array();
    foreach ($input as $a) {
    
         @$count_values[$a]++;
    
    }
    echo 'Duplicates count: '.count($count_values);
    print_r($count_values);
    ?>
    
    0 讨论(0)
  • 2020-11-27 04:24

    I came here from google looking for a way to count the occurence of duplicate items in an array. Here is the way to do it simply:

    $colors = array("red", "green", "blue", "red", "yellow", "blue");
    $unique_colors = array_unique($colors);
    // $unique colors : array("red", "green", "blue", "yellow")
    $duplicates = count($colors) - count($unique_colors);
    // $duplicates = 6 - 4 = 2
    if( $duplicates == 0 ){
     echo "There are no duplicates";
    }
    echo "No. of Duplicates are :" . $duplicates;
    
    // Output: No. of Duplicates are: 2
    

    How array_unique() works?

    It elements all the duplicates. ex: Lets say we have an array as follows -

    $cars = array( [0]=>"lambo", [1]=>"ferrari", [2]=>"Lotus", [3]=>"ferrari", [4]=>"Bugatti");

    When you do $cars = array_unique($cars); cars will have only following elements. $cars = array( [0]=>"lambo", [1]=>"ferrari", [2]=>"Lotus", [4]=>"Bugatti");

    To read more: https://www.w3schools.com/php/func_array_unique.asp

    Hope it is helpful to those who are coming here from google looking for a way to count duplicate values in array.

    0 讨论(0)
  • 2020-11-27 04:25

    this code will return duplicate value in same array

    $array = array(12,43,66,21,56,43,43,78,78,100,43,43,43,21);
    foreach($arr as $key=>$item){
      if(array_count_values($arr)[$item] > 1){
         echo "Found Matched value : ".$item." <br />";
      }
    }
    
    0 讨论(0)
提交回复
热议问题