Re: natsort()
| From: | Dave Hamber | Date: | Thu, 26 Oct 2006 21:39:53 +0000 |
| Subject: | Re: natsort() | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-243709@lists.php.net to get a copy of this message | ||
natsort() places the array elements in natural order but not the keys.
If you want your elements printed using "print" in a loop either reorganise the keys first or use "foreach".
The easiest method would be to use:
foreach($dl as $filename){
print $filename;
}
If you insist on using a while loop you could use:
$dl = array_merge($dl); to reorder the keys from 0 to array size-1.
then use:
while ($i <= $array_count){print $dl[$i]; $i++;}
If you use "while" you must increment $i to get all of the elements printed.
Alternatively you could use a for loop after reordering the keys:
for($i=0;$i<sizeof($dl);$i++){
print $dl[$i];
}
On 26/10/2006 22:05 Sandy wrote:
Hi
php5
<code>
$d = '/somedir/subdir';
$od = opendir($d);
if ($od) {
$dl = scandir($d);
natsort($dl);
}
</code>
The sorted array is available through print_r().
How can I obtain a natsorted array that can be listed using :
while ($i <= $array_count){print $dl[$i]}
Thanks