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

时间:2017-08-18 19:05:27

标签: php arrays counting

我正在尝试获取计数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);

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

3 个答案:

答案 0 :(得分:1)

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

$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);

这将返回如下内容: -

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
        )

)

答案 1 :(得分: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,
  ),
)

答案 2 :(得分:0)

你可以这样做: -

$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