php 二维数组按某个字段排序

主要用到两个函数:

1.array_column() 返回输入数组中某个单一列的值。php版本5.5以后自带

2.array_multisort() 函数返回排序数组。您可以输入一个或多个数组。函数先对第一个数组进行排序,接着是其他数组,如果两个或多个值相同,它将对下一个数组进行排序。

<?php
$data = [[
    "path" => "sftp/outgoing/1234_20200201_bill(1).csv",
    "timestamp" => 1591669515,
    "type" => "file",
    "visibility" => "public",
    "size" => 765,
    "dirname" => "sftp/outgoing",
    "basename" => "1234_20200201_bill(1).csv",
    "extension" => "csv",
    "filename" => "1234_20210201_bill(1)",
], [
    "path" => "sftp/outgoing/1234_20200611_bill.csv",
    "timestamp" => 1591937136,
    "type" => "file",
    "visibility" => "public",
    "size" => 240,
    "dirname" => "sftp/outgoing",
    "basename" => "1234_20200611_bill.csv",
    "extension" => "csv",
    "filename" => "1234_20200611_bill",
]];
$last_names = array_column($data, ‘filename‘);
array_multisort($last_names, SORT_ASC, $data);//SORT_ASC 按照上升顺序排序, SORT_DESC 按照下降顺序排序

echo ‘<pre>‘;
print_r($data);
echo ‘<pre>‘;
exit;

参考:https://www.cnblogs.com/wenzheshen/p/9455554.html

相关推荐