-1
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))
8
  • 1
    Where is your code? StackOverflow is not a code writing service. Commented Jan 25, 2018 at 5:32
  • yes sir i couldn't add properly Commented Jan 25, 2018 at 5:37
  • my code is $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)); Commented Jan 25, 2018 at 5:38
  • Please add your code in your question. Commented Jan 25, 2018 at 5:39
  • i want to find a minimum and maximum values for each package group..please help me sir Commented Jan 25, 2018 at 5:39

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Simply use the min() and max() functions.

Your code:

foreach(array_column($array, 'value') as $key => $values){
    echo PHP_EOL . 'SubArray '. $key .' min = '. min($values) . ' and max value = '.  max($values);
}

output is:

SubArray 0 min = 5 and max value = 3356143
SubArray 1 min = 1 and max value = 2764815

References:

Live demo: https://eval.in/941702

1 Comment

array_column() is not necessary if you are going to iterate the array anyhow. All eval.in links are now dead.
1

@Arebhy Sri, You should search about array in php, It's like basic problem.

$finalArray = [];    
foreach ($mainArr as  $subArr){
        $array = $subArr['value'];
        sort($array);

        $tempArray['minimum'] = reset($array);
        $tempArray['maximum'] = end($array);
        $tempArray['package'] = $subArr['package'];
        $finalArray[] = $tempArray;
    }



$finalArray //you can use

I am using simple foreach and two functions of array reset() and end().

reset(): Returns the value of the first array element, or FALSE if the array is empty.

end(): Returns the value of the last element or FALSE for empty array.

1 Comment

thank you sir..but i want to find a minimum and maximum values for each package group..please help me sir [package]=>LTE_15AGB minimum=xxxx maximum=xxxx [package]=>LTE_5AGBNT minimum=xxxx maximum=xxxx sorry for the trouble.. im newbie this is what i tried code $resultnew = array_reduce($output, function($memo, $el) { if($memo[0] < $el['value']) { $memo = array($el['value'], $el); } return $memo; }, array(0, null)); print_r($resultnew[1])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.