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>"; ?> Link to comment https://forums.phpfreaks.com/topic/110739-solved-an-easier-way/ 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. Link to comment https://forums.phpfreaks.com/topic/110739-solved-an-easier-way/#findComment-568106 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. Link to comment https://forums.phpfreaks.com/topic/110739-solved-an-easier-way/#findComment-568109 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! Link to comment https://forums.phpfreaks.com/topic/110739-solved-an-easier-way/#findComment-568111 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.