按ASC顺序对数组进行排序,具体取决于两个键值?(Sort array in ASC order depending on two key values?)

编程入门 行业动态 更新时间:2024-10-28 05:21:58
按ASC顺序对数组进行排序,具体取决于两个键值?(Sort array in ASC order depending on two key values?)

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

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查询。 提前致谢。

I have one array that is stored in one variable. The array is following:

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

I have done with sorting of array in ascending with employee_name that is running very fine with this code:

$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.

Now what i want is that every employee_name should be ascending order and each employee_name today_date should also be in ascending order. My expected output is like that:

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

Please help me how to resolve this problem. I will not go with SQL query for some reasons. Thanks in advance.

最满意答案

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

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

This works fine based on your output requirement. Try this:

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

Output:-https://eval.in/935967

更多推荐

本文发布于:2023-07-22 06:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1217911.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   键值   顺序   两个   ASC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!