Question

I'm ajaxing an array of images from a file with php opendir function. It works in-order locally but spits out randomly when I upload it on my server. I thought maybe because I'm not ordering it personally it's doing this. I tried using the natsort() on my array before I send it back with JSON_ENCODE but it doesn't work because it's returning an object (and it's still in the wrong order just numbered). I get this locally from console.log():

 ["01_cul_project_01.jpg", "02_cor_project_02.jpg", "03_cul_project_03.jpg",...]

And this from my server from console.log():

 0: "07_cor_project_07.jpg"
 1: "08_cul_project_08.jpg"
 2: "15_cor_project_15.jpg"
 ...

Are there any better ways of ordering the array? Any suggestions in how to fix this? Cheers in advance for your help.

EDIT 01 - SOLUTION

Sort the array after JSON callback with javascript:

 var blah = array("3","1","5"...);
 blah.sort();
Was it helpful?

Solution

Sort the array after JSON callback with javascript:

var blah = ["3", "1", "5"...];
blah.sort();

gives

["1", "2", "3"...]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top