Count duplicate values in multidimensional array

后端 未结 2 1689
别那么骄傲
别那么骄傲 2021-01-25 05:10

I need to count the same values in multidimensional array and remove the duplicates.

My array:

$r = [
    [\'a\',\'b\'],
    [\'a\',\'b\'],
    [\'c\',\'         


        
相关标签:
2条回答
  • 2021-01-25 05:35
    <?php
    $r = [
        ['a','b'],
        ['a','b'],
        ['c','d'],
        ['c','d'],
        ['c','d'],
        ['e','f'],
    ];
    foreach($r as $arr)
    {
      $o[implode(',', $arr)][] = 1;
    }
    $output = [];
    array_walk($o, function($v, $k) use(&$output){
        $output[] = array_merge(explode(',', $k), [count($v)]);
    });
    var_dump($output);
    

    and the output:

    array(3) {
      [0]=>
      array(3) {
        [0]=>
        string(1) "a"
        [1]=>
        string(1) "b"
        [2]=>
        int(2)
      }
      [1]=>
      array(3) {
        [0]=>
        string(1) "c"
        [1]=>
        string(1) "d"
        [2]=>
        int(3)
      }
      [2]=>
      array(3) {
        [0]=>
        string(1) "e"
        [1]=>
        string(1) "f"
        [2]=>
        int(1)
      }
    }
    
    0 讨论(0)
  • 2021-01-25 05:43
    foreach ( $result1 as $key ):
        $o[implode(', ', $key)][] = null;
         foreach ($o as $key1) {
            $g[implode(', ', $key)] = count($key1);
        }
    endforeach;
    print_r($g);
    
    0 讨论(0)
提交回复
热议问题