Array sorting in ASC order depending on two key values?

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

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 did with sorting the array in ascending order with employee_name, which works very well 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 I want everyone employee_nameto be incremental, and everyone employee_name today_datealso to be in ascending order. My expected result is as follows:

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 solve this problem. For some reason, I will not use the SQL query. Thanks in advance.

+4
source share
1 answer

This works just fine based on your exit requirement. Try the following:

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

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

+4
source

Source: https://habr.com/ru/post/1692291/


All Articles