Jump to content

min value of nested arrays


dadamssg

Recommended Posts

I have a nested array. I'm trying to write code to produce the minimum value foreach first key of the $test array. So, ideally it would produce what's in the $result array. I don't know why i can't figure out how to do this. The multiple nests are throwing me off. I think i'm over thinking this.

 

<?php

$test['abcd']['0'] = array('10','100','300');
$test['abcd']['1'] = array('1000','100','3000');
$test['abcd']['3'] = array('23','89','700');

$test['efgh']['0'] = array('3','55','300');
$test['efgh']['1'] = array('160','160','9000');
$test['efgh']['3'] = array('900','89','200');

//$result['abcd'] = 10;
//$result['efgh'] = 3;

?>

Link to comment
https://forums.phpfreaks.com/topic/260663-min-value-of-nested-arrays/
Share on other sites

There's probably a more intuitive way but whatever.

foreach($test as $name => $arr) {
$tmp = array_shift($arr); //get first element of $test[$name], which is always $test[$name]['0'] in your example
sort($tmp);
$result[$name] = $tmp[0];
}

print_r($result);

Array
(
    [abcd] => 10
    [efgh] => 3
)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.