mkosmosports Posted March 25, 2007 Share Posted March 25, 2007 Hey, Ive got this array Array ( [4] => Array ( [fullname] => Marek Kosmider [title] => Arsenal: Why do they lose so often? [adate] => 2007-03-11 [acontent] => articles/20070311_Kosmider_Arsenal_why.html ) [3] => Array ( [fullname] => Marek Kosmider [title] => Chelsea: Complete sheit! [adate] => 2007-03-11 [acontent] => articles/20070311_Kosmider_Chelsea.html ) [2] => Array ( [fullname] => Orlando Monchez [title] => Carlos Tevez: Not the West Ham saviour after all [adate] => 2007-01-07 [acontent] => articles/20070107_Monchez_Tevez.html ) ) How can I pull out the max 2nd level array key which would be 4 in this case? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/ Share on other sites More sharing options...
cmgmyr Posted March 25, 2007 Share Posted March 25, 2007 so you want : [acontent] => articles/20070311_Kosmider_Arsenal_why.html right? Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214815 Share on other sites More sharing options...
mkosmosports Posted March 25, 2007 Author Share Posted March 25, 2007 Hey, No, Im actually looking for the maximum numeric key, which would be 4.... Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214818 Share on other sites More sharing options...
cmgmyr Posted March 25, 2007 Share Posted March 25, 2007 o... $your_array; $max = max($your_array); Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214819 Share on other sites More sharing options...
mkosmosports Posted March 25, 2007 Author Share Posted March 25, 2007 But in my case its a multidimensional array, so using $max = max($my_array) results in Array... Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214823 Share on other sites More sharing options...
cmgmyr Posted March 25, 2007 Share Posted March 25, 2007 try this: <?php function multimax( $array ) { // use foreach to iterate over our input array. foreach( $array as $value ) { // check if $value is an array... if( is_array($value) ) { // ... $value is an array so recursively pass it into multimax() to // determine it's highest value. $subvalue = multimax($value); // if the returned $subvalue is greater than our current highest value, // set it as our $return value. if( $subvalue > $return ) { $return = $subvalue; } } elseif($value > $return) { // ... $value is not an array so set the return variable if it's greater // than our highest value so far. $return = $value; } } // return (what should be) the highest value from any dimension. return $return; } ?> *found it on php.net : http://us2.php.net/max Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214824 Share on other sites More sharing options...
mkosmosports Posted March 25, 2007 Author Share Posted March 25, 2007 Hey, Ive actually figured out another way to do this which looks less complicated then the one php.net suggests. So ive got my array: Array ( [4] => Array ( [fullname] => Marek Kosmider [title] => Arsenal: Why do they lose so often? [adate] => 2007-03-11 [acontent] => articles/20070311_Kosmider_Arsenal_why.html ) [3] => Array ( [fullname] => Marek Kosmider [title] => Chelsea: Complete sheit! [adate] => 2007-03-11 [acontent] => articles/20070311_Kosmider_Chelsea.html ) [2] => Array ( [fullname] => Orlando Monchez [title] => Carlos Tevez: Not the West Ham saviour after all [adate] => 2007-01-07 [acontent] => articles/20070107_Monchez_Tevez.html ) ) Then I run a loop to pull out the ids, place them into another array and that way I can easily get the max.. foreach($artarr as $key => $value) { $ids[] = $key; } So, $ids[0] would be the max. Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214831 Share on other sites More sharing options...
cmgmyr Posted March 25, 2007 Share Posted March 25, 2007 i guess you could always sort it, count it, then output the last one too ...as long as it works Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214832 Share on other sites More sharing options...
mkosmosports Posted March 25, 2007 Author Share Posted March 25, 2007 True enough. Quote Link to comment https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214844 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.