# Sorting Array

Go to solution Solved by requinix,

## Recommended Posts

Greetings all,

I have a script that creates an array from a folder of images.

```// Create Array
\$files = array();
if( is_dir( \$thumbs_dir ) ) {
if( \$handle = opendir( \$thumbs_dir ) ) {
while( ( \$file = readdir( \$handle ) ) !== false ) {
if( \$file != "." && \$file != ".." ) {
array_push( \$files, \$file );
}
}
closedir( \$handle );
}
}
```

If I echo this array out into a table or what have you it lists them in a strange way.

4_Photo1.jpg

4_Photo11.jpg

4_Photo12.jpg

......and so on up to 19

4_Photo2.jpg

4_Photo21.jpg

4_Photo22.jpg

.....and so on up to 29

4_Photo3.jpg

4_Photo31.jpg

4_Photo32.jpg

As you can see the array comes out in, well, a numeric order but a weird one. Instead of loading 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, it loads like above 1, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 21, 22.....

How can I go about sorting this so that it comes out how one would count normally? Preferably in reverse order, counting down would be better.

I tried:

sort( \$files, SORT_NUMERIC );

But that got really messy and threw the order all over the place.

Best Regards,

Nightasy

##### Share on other sites

• Solution

1. You can replace most of that code with one call to glob

2. The filenames are being stored as if they were strings. Which they are. "Photo19" sorts before "Photo2" because 1 comes before 2. Try sorting the array differently.

```P = P
h = h
o = o
t = t
o = o
1 < 2
9```
##### Share on other sites

Yea, I just had tried natsort(\$files); shortly after making this post and it seems my images are echoing out weirdly for a different reason.

natsort is working but I'll have to look over my code as the images are echo'n out of the array weirdly for some other reason.

Thanks for the help.

Edited by Nightasy
##### Share on other sites

After doing a little fooling around with my script I discovered that although the natsort is sorting my array the way I want it to be sorted I also need it reindexed with the new sort.

Using this:

```natsort(\$files);
\$files = array_reverse(\$files, true);
```

I get this with a print_r(\$file):

Array (

[16] => 4_Photo24.jpg

[15] => 4_Photo23.jpg

[14] => 4_Photo22.jpg

[13] => 4_Photo21.jpg

[12] => 4_Photo20.jpg

[10] => 4_Photo19.jpg

[9] => 4_Photo18.jpg

[8] => 4_Photo17.jpg

[7] => 4_Photo16.jpg

[6] => 4_Photo15.jpg

[5] => 4_Photo14.jpg

[4] => 4_Photo13.gif

[3] => 4_Photo12.jpg

[2] => 4_Photo11.jpg

[1] => 4_Photo10.jpg

[23] => 4_Photo9.jpg

[22] => 4_Photo8.jpg

[21] => 4_Photo7.jpg

[20] => 4_Photo6.jpg

[19] => 4_Photo5.jpg

[18] => 4_Photo4.jpg

[17] => 4_Photo3.jpg

[11] => 4_Photo2.jpg

[0] => 4_Photo1.jpg )

As you can see, the numbers for the photos are in the correct reversed order. But they are not indexed in the correct order.

How would I go about reindexing the new order created from the natsort?

Edited by Nightasy
##### Share on other sites

I figured it out. Nevermind.

```natsort(\$files);
\$files = array_reverse(\$files, true);
\$files = array_values(\$files);
```
##### Share on other sites

Have you checked what the second argument to array_reverse() means? Why you're passing a `true` there? Because as it turns out you don't want to do that, making the array_values() unnecessary.

##### Share on other sites

Have you checked what the second argument to array_reverse() means? Why you're passing a `true` there? Because as it turns out you don't want to do that, making the array_values() unnecessary.

Thanks for the heads up. I'm pretty new to really playing with arrays. They covered arrays in my college course but for me it takes playing with this stuff outside of course material to really learn it. One code at a time I suppose.

I really just needed the index to match the natsorted array in order to make use of a \$files[\$i] type loop further down in my script.

Funny thing though, it was working with it set to true. I removed the true and it's still working exactly how it is supposed to. The example in the manual doesn't make much sense to me about how that true bool affects it though. Maybe I'm just tired lol. Regardless, it works one way or the other for my purposes so I just removed it.

Edit:

I mean, if you think about it. It wouldn't matter because I'm immediately following the array_reverse with array_values so it's all getting re-indexed anyways. Right?

Edited by Nightasy
##### Share on other sites

Funny thing though, it was working with it set to true. I removed the true and it's still working exactly how it is supposed to. The example in the manual doesn't make much sense to me about how that true bool affects it though. Maybe I'm just tired lol. Regardless, it works one way or the other for my purposes so I just removed it.

That second argument is about not reindexing. You want the default behavior.

I mean, if you think about it. It wouldn't matter because I'm immediately following the array_reverse with array_values so it's all getting re-indexed anyways. Right?

I could drive across the country for some ice cream, but why would I want to do that when there's a grocery store down the block?
##### Share on other sites

Oh I see now. I removed the true from the argument and just to test, so I better understood it, I also removed the \$files = array_values(\$files);

I didn't need to use array_values provided I removed the true from the second argument. It makes sense now after a good nights sleep.

##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.