简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM