phparraysmaxminimumarray-reduce

find the minimum and maximum value in array group php


Array
(
    [0] => Array
        (
            [package] => LTE_15AGB
            [value] => Array
                (
                    [0] => 52690
                    [1] => 24700
                    [2] => 43972
                    [3] => 506417
                    [4] => 488125
                    [5] => 935918
                    [6] => 1322816
                    [7] => 1189040
                    [8] => 2805279
                    [9] => 2764825
                    [10] => 1688294
                    [11] => 1228812
                    [12] => 2232345
                    [13] => 3356143
                    [14] => 1193213
                    [15] => 167589
                    [16] => 1373104
                    [17] => 691411
                    [18] => 1398647
                    [19] => 5
                )

        )

    [1] => Array
        (
            [package] => LTE_15AGB_NT
            [value] => Array
                (
                    [0] => 953370
                    [1] => 151168
                    [2] => 37605
                    [3] => 428769
                    [4] => 755222
                    [5] => 1146719
                    [6] => 494289
                    [7] => 889002
                    [8] => 307200
                    [9] => 127972
                    [10] => 2764815
                    [11] => 1426224
                    [12] => 262669
                    [13] => 648757
                    [14] => 1485
                    [15] => 1202
                    [16] => 998
                    [17] => 1
                )

        )

)

This is what I have tried:

$tmp = array();
foreach($arrayName as $arg){ 
    $tmp[$arg['package']][] = $arg['value']; 
}

$output = array();
foreach($tmp as $type => $labels){
    $output[] = array( 'package' => $type, 'value' => $labels, ); 
}

print_r(($output))

Solution

  • Try this:

    foreach ($your_array as $subarr) {
        echo $subarr[package]." minimum = ";
        echo min($subarr[value])." and maximum = ";
        echo max($subarr[value])."<br>";
     }
    

    this will output each package name together with the minimum and maximum values.