Home > Back-end >  How do I get the highest value from value in PHP multidimensional array?
How do I get the highest value from value in PHP multidimensional array?

Time:11-01

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?

CodePudding user response:

    <?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.

CodePudding user response:

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,
  ),
)

CodePudding user response:

Since PHP 5.5 you can do the following:

echo max(array_column($data, 'tahun'));
  • Related