Jump to content

[SOLVED] An easier way?


rarebit

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.