dadamssg Posted April 10, 2012 Share Posted April 10, 2012 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/260663-min-value-of-nested-arrays/ Share on other sites More sharing options...
caliux Posted April 10, 2012 Share Posted April 10, 2012 This is an array with 3 dimensions.Try to make a new array and concanate all elements form $test['abcd'][0],1,2,3 etc and then sort the new arrray. Quote Link to comment https://forums.phpfreaks.com/topic/260663-min-value-of-nested-arrays/#findComment-1335961 Share on other sites More sharing options...
dcro2 Posted April 10, 2012 Share Posted April 10, 2012 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 ) Quote Link to comment https://forums.phpfreaks.com/topic/260663-min-value-of-nested-arrays/#findComment-1336050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.