How to sort a multi dimensional array in PHP alphabetically?

后端 未结 2 1966
误落风尘
误落风尘 2021-01-26 07:36

My API written in PHP (Codeigniter) outputs users based on a selected keyword how can I sort this array in alphabetical order before it outputs to JSON.

This is the outp

2条回答
  •  温柔的废话
    2021-01-26 08:28

    This one works. Tried, tested, and true:

    function sort_by_lastname($a, $b) {
        $a = trim($a['user']['basic'][0]['lastname']);
        $b = trim($b['user']['basic'][0]['lastname']);
        return strcmp($a,$b);
    }
    
    uasort($array['contacts'],'sort_by_lastname');
    

提交回复
热议问题