adrianTNT Posted December 5, 2009 Share Posted December 5, 2009 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" , 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? Quote Link to comment https://forums.phpfreaks.com/topic/184121-shocked-while-trying-to-sort-a-simple-array/ Share on other sites More sharing options...
adrianTNT Posted December 5, 2009 Author Share Posted December 5, 2009 ok, I guess I never noticed the difference between arsort() and rsort() Now I have to review thousand of code lines that I made so far and used arrays EDIT: Quote Link to comment https://forums.phpfreaks.com/topic/184121-shocked-while-trying-to-sort-a-simple-array/#findComment-972110 Share on other sites More sharing options...
salathe Posted December 5, 2009 Share Posted December 5, 2009 Note: It seems you've solve this already but I typed a reply so I'm going to post it! 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. Quote Link to comment https://forums.phpfreaks.com/topic/184121-shocked-while-trying-to-sort-a-simple-array/#findComment-972115 Share on other sites More sharing options...
adrianTNT Posted December 5, 2009 Author Share Posted December 5, 2009 Yes, that was the problem. I have to review so much code now Quote Link to comment https://forums.phpfreaks.com/topic/184121-shocked-while-trying-to-sort-a-simple-array/#findComment-972120 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.