rarebit Posted June 18, 2008 Share Posted June 18, 2008 Hi, never really tried this, but basically i'm just after the first elements 'key' and 'value' after sorting the array. Here's an example, the foreach set does it but then breaks, i'm sure if thats too messy and takes longer. Then theres the second way with 'reset' and 'each', but that seems perverse... Is there a better way? <?php $res = array('M'=>31, 'Y'=>1, 'D'=>365); asort($res); //print_r($res); //print "<br>"; // VERSION 1 foreach($res as $k => $v) { print $k.": ".$v."<br>"; break; } // VERSION 2 reset($res); $a = each($res); print $a['key'].": ".$a['value']."<br>"; ?> Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted June 18, 2008 Share Posted June 18, 2008 version one breaks because you have the break; in the loop. Take it out an it should work. Quote Link to comment Share on other sites More sharing options...
Orio Posted June 18, 2008 Share Posted June 18, 2008 I think this should be enough: <?php $res = array('M'=>31, 'Y'=>1, 'D'=>365); asort($res); reset($res); echo key($res).": ".current($res)."<br>"; ?> Orio. Quote Link to comment Share on other sites More sharing options...
rarebit Posted June 18, 2008 Author Share Posted June 18, 2008 Thanks guys, yes Orio, I had had a play with current, but messed it up... The Little Guy, I only want the first element... Cheers, have a good day! Quote Link to comment 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.