0

If this has been answered before I apologize (I haven't seen to found it). I have a game pool website that wishes to sort by points (pts). In the below example I want 'Team B' to be first as their points are higher then Team A's at 22 compared to 12. While the solution on this page (How to sort an array of associative arrays by value of a given key in PHP?) seemed to be very similar to what I was looking for I wasn't able to get it to work.

Array
(

[0] => Team Object
    (
        [id] => 5
        [name] => Team A
        [games_played] => 13
        [wins] => 6
        [losses] => 7
        [ot_losses] => 0
        [pts] => 12
        [goals_for] => 7.5
        [goals_against] => 22
        [streak] => 6-7
    )

[1] => Team Object
    (
        [id] => 2
        [name] => Team B
        [games_played] => 13
        [wins] => 11
        [losses] => 2
        [ot_losses] => 0
        [pts] => 22
        [goals_for] => 51
        [goals_against] => 19
        [streak] => 11-2
    )

I suspect I'm on the right path with the following but missing something...

$new_array = array();
foreach ($array_objects as $key => $row)
{
    $new_array[$key] = $row['pts'];
}
array_multisort($price, SORT_DESC, $array_objects);
soma56
  • 59
  • 1
  • 8
  • `$price` should be `$new_array`. – Barmar Oct 31 '18 at 21:41
  • 1
    Looks like you copied the code from https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php but forgot to rename the variable everywhere. – Barmar Oct 31 '18 at 21:41
  • 1
    Personally, I prefer the `usort` answer there anyway. And if this data comes from your db, adding an ORDER BY to your query will be better than sorting it in PHP. – Don't Panic Oct 31 '18 at 21:45
  • Possible duplicate of [How to sort an array of associative arrays by value of a given key in PHP?](https://stackoverflow.com/questions/1597736/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php) – miken32 Oct 31 '18 at 22:32

0 Answers0