Jump to content

using max...


mkosmosports

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/44232-using-max/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214824
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/44232-using-max/#findComment-214831
Share on other sites

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.