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,
)
)
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();
Using array_multisort()
.
$array = array(
46 =>
array (
'name' => 'HSR Layout',
'url' => 'hsr-layout',
'count' => 2,
),
37 =>
array (
'name' => 'Electronic City',
'url' => 'electronic-city',
'count' => 3,
)
);
$price = array();
foreach ($array as $key => $row)
{
$count[$key] = $row['count'];
}
array_multisort($count, SORT_DESC, $array);
print_r($array);
Program Output
Array
(
[0] => Array
(
[name] => Electronic City
[url] => electronic-city
[count] => 3
)
[1] => Array
(
[name] => HSR Layout
[url] => hsr-layout
[count] => 2
)
)
Live demo : Click Here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With