简体   繁体   中英

sorting a multidimensional array with multiple sorts in PHP

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.

array_multisort(array_column($filter_arr, 'Route'), array_column($filter_arr, 'deviceName'),  SORT_ASC, array_column($filter_arr, 'startTime'), SORT_ASC, $filter_arr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM