Natural sort an associative array?

后端 未结 3 1084
失恋的感觉
失恋的感觉 2020-12-18 06:18

Given an array of arrays, how would I natural sort the inner arrays based on their values?

Example array:

array (size=2)
  0 => 
    array (size=1         


        
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-18 06:30

    PHP's usort() allows you to define a custom callback function for sorting:

    usort($array, function($a, $b){
        if ($a['manager'] == $b['manager']) {
            return 0;
        }
        return ($a['manager'] < $b['manager']) ? -1 : 1;
    });
    

    For sorting months, you can use something like date('m', strtotime($a['month'])) to get the month number for comparison. More ways to do that can be found here: convert month from name to number

提交回复
热议问题