Jump to content

Shocked while trying to sort a simple array


adrianTNT

Recommended Posts

Hello. I was trying to sort values of an array and something is very different from everything I knew so far.

 

Code is:

$files_list = array();
array_push($files_list,"0.jpg");
array_push($files_list,"3.jpg");
array_push($files_list,"1.jpg");
array_push($files_list,"2.jpg");

arsort($files_list);

echo $files_list[0];

 

I was expecting last line to echo "3.jpg" but it keeps returning "0.jpg"  :o, I tried all kind of sort/arsort, nothing retruns that greatest value, what am I doing wrong?

I managed to find a solution by end($array_name), but still what was wrong with the above code?  :o

Note: It seems you've solve this already but I typed a reply so I'm going to post it!  :shy:

 

arsort won't help you because although it does sort the items, the keys are preserved meaning that (as you can see) index zero still belongs with the value 0.jpg. sort won't help because it does re-index the array but sorts in ascending order.  What you're probably after is rsort (rsort($files_list, SORT_NUMERIC);). With that, the sorted $files_list[0] will be 3.jpg.

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.