Vikash Vikash - 5 months ago 10
PHP Question

How to sort an assocoative array in php

I want to sort this array based on count in descending order. here is my

array


array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),

37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
)

Answer

If you are using Laravel, which your tag suggests, you can use collections to manipulate arrays like this. For example:

$array = collect($array)->sortBy('count')->reverse()->toArray();