Sorting Associative Arrays in PHP

$arr = [

'foo' => 'bar',
'faz' => 'baz'

];


array_multisort(array_column($arr, 'value'), SORT_ASC, $arr);

You can sort an associative array with one line of code as I did above. As a result, the array becomes sorted by the keys:

[ 
   'faz' => 'baz', 
   'foo' => 'bar' 
]

 

Leave a Reply

Your email address will not be published. Required fields are marked *