0

I need to loop through and print all nested data within the "players" array and sort by "wonAmount." I can get my PHP to print out a single value, which I target, but I can't get it to work when I try the foreach loop. So instead, it prints out "array."

JSON

{
  "tournamentID": 65,
  "gameType": "Holdem",
  "name": "TRI KRALJA ZA EVROPU",
  "start": "2022-04-07 13:30:00",
  "status": "Finished",
  "buyIn": 100000,
  "entryFee": 10000,
  "guaranteedPrize": 12500000,
  "rebuyFee": 100000,
  "rebuyRake": 10000,
  "addonFee": 0,
  "addonRake": 0,
  "tableSize": 9,
  "speed": "Turbo",
  "players": [
    {
      "id": 1000000,
      "alias": "demo1",
      "rank": 1,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 6250000
    },
    {
      "id": 1000005,
      "alias": "demo6",
      "rank": 2,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 3750000
    },
    {
      "id": 1000087,
      "alias": "demo10",
      "rank": 3,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 2500000
    },
    {
      "id": 1000008,
      "alias": "demo9",
      "rank": 4,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000078,
      "alias": "demo15",
      "rank": 5,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000002,
      "alias": "demo3",
      "rank": 6,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000006,
      "alias": "demo7",
      "rank": 7,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000090,
      "alias": "demo13",
      "rank": 8,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000171,
      "alias": "demo14",
      "rank": 9,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000088,
      "alias": "okokoke",
      "rank": 10,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000007,
      "alias": "demo8",
      "rank": 11,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000086,
      "alias": "demo11",
      "rank": 12,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000004,
      "alias": "demo5",
      "rank": 13,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000001,
      "alias": "demo2",
      "rank": 14,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000003,
      "alias": "demo4",
      "rank": 15,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    },
    {
      "id": 1000077,
      "alias": "helloworld",
      "rank": 16,
      "rebuyCount": 0,
      "addonCount": 0,
      "wonAmount": 0
    }
  ]
}
2
  • The array elements are also arrays. You need to print the elements like echo $player['id'], or use print_r($player) instead of echo $player. Commented Apr 7, 2022 at 21:14
  • If you want more specific help, post your code. Commented Apr 7, 2022 at 21:15

1 Answer 1

1

first, you need to decode the json using json_decode then you need to sort & print the player's array

$data = json_decode($json_data,true);

//extract the players from json 
$players = $data["players"];

//sort players array
$wonAmount = array();
foreach ($players as $key => $row)
{
    $wonAmount[$key] = $row['wonAmount'];
}
array_multisort($wonAmount, SORT_DESC, $players);


//print players data 

foreach ($players as $player)
{
    //add here any keys you want to print from player array 
    echo $player["id"]; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I came up with something similar but you beat me to it. I find array_multisort very confusing so I used usort: usort($players, function($a, $b) { return $a['wonAmount'] - $b['wonAmount']. Also confusing but slightly less so to me.

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.