Re: natsort ?
[email protected] (Ben Ramsey)
| Newsgroups | php.general |
|---|---|
| Message-ID | <[email protected]> |
On 5/22/22 11:00, JEFFRY KILLEN wrote:
> Hello:
> I am trying to get an array sorted naturally.
>
> I do something like
>
> $list = scandir($dirname);
> natsort($list);
>
> According to the manual page the array values are sorted but
> the keys remain the same. So running the array through a loop
> does not produce the sorted order. I produces the original order.
>
> How to I get a sorted version of the array that I can loop through
> and produce the revised order?
>
> journal-1.2020.php
> journal-10.2020.php
> journal-11.2020.php
> journal-12.2020.php
> journal-2.2020.php
> journal-3.2020.php
> journal-4.2020.php
> journal-5.2020.php
> journal-6.2020.php
> journal-7.2020.php
> journal-8.2020.php
> journal-9.2020.php
>
> Thank you for time and attention
> Jeff K.
>
How are you looping over the array?
If you use a for-loop with a counter, then you will see the elements in
the array in order of the count.
If you use a foreach-loop, then you'll see the elements in their order
in the array (the sorted order).
Does this produce the expected output?
foreach ($list as $item) {
echo "$item\n";
}
--
Cheers,
Ben