微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个存储在一个变量中的数组.该阵列如下:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐