24

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, ) ) 
2

2 Answers 2

76

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(); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Jerodev. I am using laravel. It works :)
It Works great, and if I want to be order by month ? how it would be ?
5

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

4 Comments

check above code its useful to you for your issue. @Vikash
This is much too complicated, a simple usort with a count comparing lambda-function is all that's needed here
@RuchishParikh as OP says he is using laravel your answer is not useful for him. Also a complex answer is not needed if you want to give it too also:- stackoverflow.com/questions/2699086/…
ok i will keep in mind. Thanks for your advice @Anant

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.