sKunKbad Posted April 20, 2009 Share Posted April 20, 2009 I need to return the key of the highest associated value: $arr = array( '0' => '456', '1' => '545', '2' => '34' ); So, I need to return 1 in this case, but am having brain blockage right now. Help! Quote Link to comment https://forums.phpfreaks.com/topic/154942-solved-get-the-key-of-highest-value-in-an-associative-array/ Share on other sites More sharing options...
Maq Posted April 20, 2009 Share Posted April 20, 2009 max maybe? EDIT: Just tested it with your sample array, should be exactly what you're looking for. Quote Link to comment https://forums.phpfreaks.com/topic/154942-solved-get-the-key-of-highest-value-in-an-associative-array/#findComment-814992 Share on other sites More sharing options...
sKunKbad Posted April 20, 2009 Author Share Posted April 20, 2009 max maybe? I figured it out. I just needed to use arsort() Well, I spoke too soon... Quote Link to comment https://forums.phpfreaks.com/topic/154942-solved-get-the-key-of-highest-value-in-an-associative-array/#findComment-814993 Share on other sites More sharing options...
alphanumetrix Posted April 20, 2009 Share Posted April 20, 2009 you could always loop it. $highest = 1; foreach ($arr as $a) { if ( $highest <= $a ) $highest = $a; } echo $highest; edit -- didn't realize this was already solved... Quote Link to comment https://forums.phpfreaks.com/topic/154942-solved-get-the-key-of-highest-value-in-an-associative-array/#findComment-815021 Share on other sites More sharing options...
sKunKbad Posted April 20, 2009 Author Share Posted April 20, 2009 Actually, it wasn't solved, but I did solve it. I'm working on a google sitemap generator for the Kohana php framework. You can see in the code below that I am checking the last modified time of all controller and view files associated with a "page", then I compare those times and output the sitemap with the most recent last modified time for each "page". There's still a lot of work to do... header("Content-Type: text/xml"); echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.google.com/schemas/sitemap/0.84 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd\">"; $pages = array( 'welcome' => array('views/welcome_content.php','controllers/welcome.php'), 'second' => array('views/second_content.php','controllers/second.php') ); foreach ($pages as $page => $parts){ for($x=0;$x<=count($parts)-1;$x++){ $filename = "C:/wamp/www/kohana/application/$parts[$x]"; $timecheck[] = filemtime($filename); } $largest = max($timecheck); $large = array_keys($timecheck, $largest); $filemtimeHome = date ("c", $timecheck["$large[0]"]); echo " <url> <loc>" . url::site("$page", 'http') . ".php</loc> <lastmod>$filemtimeHome</lastmod> </url>"; unset($timecheck); } echo"</urlset>"; Quote Link to comment https://forums.phpfreaks.com/topic/154942-solved-get-the-key-of-highest-value-in-an-associative-array/#findComment-815036 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.