0

I'm trying to sort an array from a database. I've trimmed down the fields to make it easier to read:

    ["id"]=> string(4) "3214"
    ["prep_file"]=> string(16) "Frontispiece.jpg" 
    ["chapter"]=> string(0) "" 
    ["position"]=> string(0) "" 
    ["strippedfile"]=> string(12) "Frontispiece" 
} 
[1]=> array(21) { 
    ["id"]=> string(4) "3213" 
    ["prep_file"]=> string(8) "5_21.jpg" 
    ["chapter"]=> string(0) "" 
    ["position"]=> string(0) "" 
    ["strippedfile"]=> string(4) "5_21" 
}
[1]=> array(21) { 
    ["id"]=> string(4) "3214" 
    ["prep_file"]=> string(8) "5_22.jpg" 
    ["chapter"]=> string(1) "1" 
    ["position"]=> string(0) "" 
    ["strippedfile"]=> string(4) "5_22" 
}

I would like to sort the array so that chapter is sorted ascending, then position is sorted ascending, finally strippedfile is sorted naturally.

Unfortunately, I can't seem to do this with array_multisort, it looks like I do:

array_multisort($array['chapter'], SORT_ASC, SORT_STRING, $array['position'], SORT_ASC, SORT_STRING, $array['strippedfile'], SORT_ASC, SORT_NATURAL)

but that doesn't work.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php – 04FS Nov 25 '19 at 12:44
  • Use `usort`, and write your own little comparison function, that compares two items according to your criteria. – 04FS Nov 25 '19 at 12:45

1 Answers1

-1
array_multisort(array_column($filter_arr, 'Route'), array_column($filter_arr, 'deviceName'),  SORT_ASC, array_column($filter_arr, 'startTime'), SORT_ASC, $filter_arr);
BhAvik Gajjar
  • 473
  • 3
  • 19
  • 2
    Your answers are are attracting a few downvotes. You should probably stop posting answers and figure out how to write a good answer before continuing. I see someone posted some links, also look in the help section. – Andreas Nov 25 '19 at 12:48
  • 1
    Tip: add an explanation to each piece of code, and eventually add a link to documentation – Cœur Nov 25 '19 at 17:15