string(4) "3214" ["prep_file"]=> string(" /> string(4) "3214" ["prep_file"]=> string(" /> string(4) "3214" ["prep_file"]=> string("/>

sorting a multidimensional array with multiple sorts in PHP

64 Views Asked by At

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.

1

There are 1 best solutions below

2
BhAvik Gajjar On
array_multisort(array_column($filter_arr, 'Route'), array_column($filter_arr, 'deviceName'),  SORT_ASC, array_column($filter_arr, 'startTime'), SORT_ASC, $filter_arr);