imHavoc
imHavoc

Reputation: 277

How do I get max(); to display the highest value number rather than just "Array"?

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

Upvotes: 1

Views: 182

Answers (1)

Andrew Moore
Andrew Moore

Reputation: 95424

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.

Upvotes: 4

Related Questions