0
[
    {
        "clubName": "FC Nightmare",
        "win": 0,
        "played": 1,
        "draw": 0,
        "lose": 1,
        "gs": 1,
        "ga": 2,
        "gd": -1,
        "points": 0
    },
    {
        "clubName": "Manchester City",
        "win": 1,
        "played": 1,
        "draw": 0,
        "lose": 0,
        "gs": 3,
        "ga": 2,
        "gd": 1,
        "points": 3
    },
    {
        "clubName": "Inter Milan",
        "win": 0,
        "played": 1,
        "draw": 0,
        "lose": 1,
        "gs": 2,
        "ga": 3,
        "gd": -1,
        "points": 0
    },
    {
        "clubName": "AC Milan",
        "win": 1,
        "played": 1,
        "draw": 0,
        "lose": 0,
        "gs": 2,
        "ga": 1,
        "gd": 1,
        "points": 3
    }
]

i have this array.i want to sort this array by points.how to do that in laravel??

i've tried this:

array_multisort(array_column($point_table,'points'),$point_table);

but it doesn't work..

4

2 Answers 2

1

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();

Or

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

1 Comment

$array = collect($array)->sortBy('count')->reverse()->toArray(); it gives me same array without sorting.
0

You can do it with simple php-function usort.

But also with laravel's collection:

collect($point_table)->sortBy('points');

2 Comments

collect($point_table)->sortBy('points'); this returns same array without sorting.can you tell me how to use usort in this case
Really? That's strange. Can you show your code? My code above will return you new collection with new sorted array. For convert it to array you must use ->toArray() as @sss-s said. With usort it can be like: usort($point_table, function($a, $b) { if ($a['points'] == $b['points']) return 0; return ($a['points'] > $b['points']) ? -1 : 1; });. After that, your $point_table will be contained with new sorted array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.