简体   繁体   English

在 PHP 中使用多种排序对多维数组进行排序

[英]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.我想对数组进行排序,以便章节升序排序,然后位置升序排序,最后自然排序 strippedfile。

Unfortunately, I can't seem to do this with array_multisort, it looks like I do:不幸的是,我似乎无法使用 array_multisort 来做到这一点,看起来我是这样做的:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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