Tutorial :How do I get max(); to display the highest value number rather than just “Array”?



Question:

max($caption);
gives me this:
Array

$caption is defined here:

$i = "0";       while ($i < $count) {          list($oldCaption, $year, $order) = explode("-", $galleryDirectory[$i]);          $caption[$year][$order] = str_replace("_", " ", "$oldCaption");          echo $year; //debug        echo "<br />";        $i++;       }    

$year comes out to be
2008
2009
2009

so how do I get max($caption); to give me the value of 2009 rather than Array?

also i would put the whole code, but when i tried that it turned out messy. but I will try again, so you guys can see the whole pictures


Solution:1

Use array_keys():

$years = array_keys($caption);  $maxYear = max($years);    // or the one-liner:  $maxYear = max(array_keys($caption));  

The reason why your code wasn't working is that you were comparing the values of the array $caption, which is an another array. max() compares int values, not keys nor arrays. By using array_keys() on $caption creates another array with all years as values.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »