如何从 PHP 多维数组中的值中获取最大值?

How do I get the highest value from value in PHP multidimensional array?

我在php

中有数据
$data = [
  ["tahun" => 2001, "bulan" => 1, "pembetulan" => 0],
  ["tahun" => 2002, "bulan" => 1, "pembetulan" => 0],
  ["tahun" => 2002, "bulan" => 1, "pembetulan" => 1],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 0],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 1],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 2],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 3],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 4],
  ["tahun" => 2003, "bulan" => 2, "pembetulan" => 0]
];

如何获得每个 tahunbulan 的最高 pembetulan 数据?

    <?php

$data = [
  ["tahun" => 2001, "bulan" => 1, "pembetulan" => 0],
  ["tahun" => 2002, "bulan" => 1, "pembetulan" => 0],
  ["tahun" => 2002, "bulan" => 1, "pembetulan" => 1],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 0],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 1],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 2],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 3],
  ["tahun" => 2003, "bulan" => 1, "pembetulan" => 4],
  ["tahun" => 2003, "bulan" => 2, "pembetulan" => 0]
];

$tahun = array();
$bulan = array();
$pembetulan = array();
foreach($data as $group){
    if(!in_array($group['tahun'], $tahun)){
        $tahun[] = $group['tahun'];
    }
    if(!in_array($group['bulan'], $bulan)){
        $bulan[] = $group['bulan'];
    }
}
$maxValue_t = 0;
$maxValue_b = 0;
foreach($data as $value){
    foreach($tahun as $t){
        if($value['tahun'] == $t){
            if($maxValue_t < $value['pembetulan']){
                $maxValue_t = $value['pembetulan'];
            }
            $pembetulan['tahun'][$t] = $maxValue_t; 
        }
    }foreach($bulan as $b){
        if($value['bulan'] == $b){
            if($maxValue_b < $value['pembetulan']){
                $maxValue_b = $value['pembetulan'];
            }
            $pembetulan['bulan'][$b] = $maxValue_b; 
        }
    }
}
echo "<pre>";print_r($pembetulan);die;
//Output
Array
(
    [tahun] => Array
        (
            [2001] => 0
            [2002] => 1
            [2003] => 4
        )

    [bulan] => Array
        (
            [1] => 4
            [2] => 4
        )

)

请检查这是否对您有帮助。

填充一个结果数组,该数组根据唯一标识所需分组的两列临时分配关联的第一级键。

迭代时,如果结果数组中不存在标识键,或者如果保留的 pembetulan 小于当前迭代行的 pembetulan 值,则将当前行保存在结果。

迭代完成后,删除带有array_values()的临时一级键。

代码:(Demo)

$result = [];
foreach ($data as $row) {
    $compositeKey = $row['tahun'] . '_' . $row['bulan'];
    if (!isset($result[$compositeKey]) || $row['pembetulan'] > $result[$compositeKey]['pembetulan']) {
        $result[$compositeKey] = $row;
    }
}
var_export(
    array_values($result)
);

输出:

array (
  0 => 
  array (
    'tahun' => 2001,
    'bulan' => 1,
    'pembetulan' => 0,
  ),
  1 => 
  array (
    'tahun' => 2002,
    'bulan' => 1,
    'pembetulan' => 1,
  ),
  2 => 
  array (
    'tahun' => 2003,
    'bulan' => 1,
    'pembetulan' => 4,
  ),
  3 => 
  array (
    'tahun' => 2003,
    'bulan' => 2,
    'pembetulan' => 0,
  ),
)