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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.