计算具有多个键的数组中的出现次数

Counting occurrences in an array that has more than 1 key

我正在尝试获取包含第 1 个键值的数组的出现次数,例如:

 $newarray[0] = [
                    'id' => '2',
                    'date' => '2016-04-22'
                    ];
 $newarray[1] = [
                    'id' => '2',
                    'date' => '2016-04-13'
                    ];
 $newarray[2] = [
                    'id' => '2',
                    'date' => '2016-04-12'
                    ];
 $newarray[3] = [
                    'id' => '1',
                    'date' => '2016-03-11'
                    ];
 $newarray[4] = [
                    'id' => '2',
                    'date' => '2016-03-05'
                    ];
 $newarray[5] = [
                    'id' => '1',
                    'date' => '2016-03-01'
                    ];

我想把它改成这样:

Array ( [0] => Array ( [id] => 1 [date] => 2016-03-11 [occurences] => 2 ) [1] => Array ( [id] => 2 [date] => 2016-04-22 [occurences] => 4 )  )

我试过这样做:

$cleanarray;
$newarray2=$newarray;
$newarray;
$k=0;
$num=1;

for($i=0; $i<count($newarray); $i++){
    for($j=1; $j<count($newarray2); $j++){
        if($newarray2[$j]["id"]==$newarray[$i]["id"]){
          $num++;
        }        

    }
    $cleanarray[$k] = [
                    'id' => $newarray[$i]["id"],
                    'date' => $newarray[$i]["date"],
                    'occurences' => $num
                ];
    $k++;
  $num=0;
}

但是很多项目重复,相同的出现但是多次,在其他情况下重复的项目(具有相同的id)会有不同的出现,所以我不知道我能做什么,我知道有一个功能:

$occurences = array_count_values($array);

但是在这种情况下它不起作用,我该如何解决?

你可以像下面那样做:-

$final_array = array();

foreach($newarray as $arr){
   if(!in_array($arr['id'],array_keys($final_array))){
      $final_array[$arr['id']] = $arr;
      $final_array[$arr['id']]['occurences'] = 1;
   }else{
     $final_array[$arr['id']]['occurences'] += 1;
   }
}
$final_array= array_values($final_array);
print_r($final_array);

输出:- https://eval.in/847242

注意:- 如果您希望最终数组为 id 的升序,请使用如下 usort() 函数:-

function cmpId($a, $b) {
    return ($a['id'] - $b['id']);
}

usort($final_array, "cmpId");
print_r($final_array);

输出:- https://eval.in/847245

我知道你在找什么,但我认为这可以解决你的问题:

$newarray[0] = [
                'id' => '2',
                'date' => '2016-04-22'
                ];
$newarray[1] = [
                'id' => '2',
                'date' => '2016-04-12'
                ];
$newarray[2] = [
                'id' => '2',
                'date' => '2016-04-12'
                ];
$newarray[3] = [
                'id' => '1',
                'date' => '2016-03-11'
                ];
$newarray[4] = [
                'id' => '2',
                'date' => '2016-03-05'
                ];
$newarray[5] = [
                'id' => '1',
                'date' => '2016-03-01'
                ];

foreach($newarray as $key => $value){
    if(isset($found[$value['id']][$value['date']])) {
        $found[$value['id']][$value['date']]++;
    } else {
        $found[$value['id']][$value['date']] = 1;
    }
}
print_r($found);

这将 return 类似于:-

Array
(
    [2] => Array
        (
            [2016-04-22] => 1
            [2016-04-12] => 2
            [2016-03-05] => 1
        )

    [1] => Array
        (
            [2016-03-11] => 1
            [2016-03-01] => 1
        )

)

在此过程中使用临时密钥将是最高效的方式。临时键简化了输出数组任务,需要更少和更快的检查。如果您希望在生成结果数组后对 id 进行排序,临时键允许一个简单的 ksort() 调用。

代码:(Demo)

$newarray=[
        ['id' => '2','date' => '2016-04-22'],
        ['id' => '2','date' => '2016-04-13'],
        ['id' => '2','date' => '2016-04-12'],
        ['id' => '1','date' => '2016-03-11'],
        ['id' => '2','date' => '2016-03-05'],
        ['id' => '1','date' => '2016-03-01']
];

foreach($newarray as $a){
    if(!isset($result[$a['id']])){
        $result[$a['id']]=array_merge($a,['occurrences'=>1]);  // use id as temp key, preserve first found date
    }else{
        ++$result[$a['id']]['occurrences'];  // only update occurrences to preserve date
    }
}
ksort($result);  // optionally sort on id ASC
var_export(array_values($result));  // remove temporary keys from first level and print to screen

输出:

array (
  0 => 
  array (
    'id' => '1',
    'date' => '2016-03-11',
    'occurrences' => 2,
  ),
  1 => 
  array (
    'id' => '2',
    'date' => '2016-04-22',
    'occurrences' => 4,
  ),
)