-2

I have data in 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]
];

How do I get the highest pembetulan data for each tahun and bulan?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136

2 Answers2

1

Populate a result array which is temporarily assigned associative first-level keys based on the two columns that uniquely identify the desired grouping.

As you iterate, if the identifying key does not exist in the result array or if the retained pembetulan is less than the currently iterated row's pembetulan value, then save the current row in the result.

When finished iterating, remove the temporary first-level keys with array_values().

Code: (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)
);

Output:

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,
  ),
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
0
    <?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
        )

)

Please check if this is helpful for you.

VishalParkash
  • 490
  • 3
  • 15