gpt4 book ai didi

php - 根据两个键值按 ASC 顺序对数组进行排序?

转载 作者:可可西里 更新时间:2023-10-31 23:01:06 25 4
gpt4 key购买 nike

我有一个存储在一个变量中的数组。数组如下:

Array
(
[0] => Array
(
[employee_name] => Amit
[today_date] => 2018-01-11
)

[1] => Array
(
[employee_name] => Amit
[today_date] => 2018-01-09
)

[2] => Array
(
[employee_name] => Amit
[today_date] => 2018-01-10
)
[3] => Array
(
[employee_name] => GURVINDER
[today_date] => 2018-01-11
)

[4] => Array
(
[employee_name] => GURVINDER
[today_date] => 2018-01-10
)
)

我已经使用 employee_name 完成了数组的升序排序,使用这段代码运行得非常好:

$attendances = "above array";
uasort($attendances, function($a, $b) {
return strcmp($a["employee_name"], $b["employee_name"]);
}); // this code is sorting in ascending order with employee_name.

现在我想要的是每个 employee_name 都应该按升序排列,每个 employee_name today_date 也应该按升序排列。我的预期输出是这样的:

Array
(
[0] => Array
(
[employee_name] => Amit
[today_date] => 2018-01-09
)

[1] => Array
(
[employee_name] => Amit
[today_date] => 2018-01-10
)

[2] => Array
(
[employee_name] => Amit
[today_date] => 2018-01-11
)
[3] => Array
(
[employee_name] => GURVINDER
[today_date] => 2018-01-10
)

[4] => Array
(
[employee_name] => GURVINDER
[today_date] => 2018-01-11
)
)

请帮我解决这个问题。由于某些原因,我不会使用 SQL 查询。提前致谢。

最佳答案

根据您的输出要求,这可以正常工作。试试这个:

array_multisort(array_column($attendances, 'employee_name'),  SORT_ASC,
array_column($attendances, 'today_date'), SORT_ASC,
$attendances);
echo "<pre>";
print_r($attendances);

输出:- https://eval.in/935967

关于php - 根据两个键值按 ASC 顺序对数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48293761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com