Reputation: 53
I'm looking to order my upload files in a specific order. I believe the default is a random upload order, but I would like to change this based on the file name, which I'm having difficulty with.
The file names would be for example;
'01 smiley'
'02 dog'
'03 cat'
Currently I used a 'Drag & Drop' multiple file upload although this just uploads in any random order, I'd like to upload it by numeric order.
Code so far (upload code works, just the order needs work)...
$count = count($sort['upload']['name']);
$in=0;
while($in<$count)
{
//upload here
$in++;
}
I think I need to sort()? before my while loop, but having difficulty getting this correct. How would I be able to sort each file into a correct order.
Many thanks.
Upvotes: 0
Views: 1423
Reputation: 4152
I don't see the point of sorting your uploads personally, because it wouldn't change a thing, but if you really need to do it then array_multisort()
is the function you're looking for.
Here is an example from PHP.net:
Sorting arrays
<?php
$ar1 = array(10, 100, 100, 0);
$ar2 = array(1, 3, 2, 4);
array_multisort($ar1, $ar2);
var_dump($ar1);
var_dump($ar2);
?>
In this example, after sorting, the first array will contain 0, 10, 100, 100. The second array will contain 4, 1, 2, 3. The entries in the second array corresponding to the identical entries in the first array (100 and 100) were sorted as well.
array(4) {
[0]=> int(0)
[1]=> int(10)
[2]=> int(100)
[3]=> int(100)
}
array(4) {
[0]=> int(4)
[1]=> int(1)
[2]=> int(2)
[3]=> int(3)
}
Source: Array Multisort on PHP.net
Upvotes: 1