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..

Vinay Patil
  • 730
  • 6
  • 17
MIZAN RIFAT
  • 31
  • 1
  • 5

2 Answers2

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);    
sss S
  • 384
  • 1
  • 7
0

You can do it with simple php-function usort.

But also with laravel's collection:

collect($point_table)->sortBy('points');
Grabatui
  • 295
  • 1
  • 9
  • collect($point_table)->sortBy('points'); this returns same array without sorting.can you tell me how to use usort in this case – MIZAN RIFAT Aug 27 '19 at 12:05
  • 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. – Grabatui Aug 27 '19 at 12:18