eqiz
eqiz

Reputation: 1591

Create multi level JSON in php

I want to know how to create a JSON array like this in php...

{  
   "roster1":{  
      "player0":{  
         "sport":"NFL",
         "firstname":"Tyrod",
         "lastname":"Taylor"
      },
      "player1":{  
         "sport":"NFL",
         "firstname":"Lamar",
         "lastname":"Miller"
      }
   }
   "roster2":{  
      "player0":{  
         "sport":"NFL",
         "firstname":"Carson",
         "lastname":"Palmer"
      },
      "player1":{  
         "sport":"NFL",
         "firstname":"David",
         "lastname":"Johnson"
      }
   }
}

I know it has to be some sort of multi-dimension array I'm assuming? I've tried something like...

$obj = new stdClass();
$y = 1;


$players["player".$x]['sport'] = "NFL";
$players["player".$x]['firstname'] = "Tyrod";
$players["player".$x]['lastname'] = "Taylor";
$x++;
    $players["player".$x]['sport'] = "NFL";
$players["player".$x]['firstname'] = "Lamar";
$players["player".$x]['lastname'] = "Miller";

But this seems like a terrible approach and that I'm doing something very noob. I really want to know the correct way of doing something like this.

Upvotes: 1

Views: 2814

Answers (2)

grimmdude
grimmdude

Reputation: 374

Ideally you wouldn't add the roster and player numbers to the key (roster1, player1, etc). I'd use the array keys to do that job for me. Something like:

 $data['rosters'][] = [
                        ['sport' => 'nfl', 'firstname' => 'Tyrod', 'lastname' => 'Taylor'],
                        ['sport' => 'nfl', 'firstname' => 'Lamar', 'lastname' => 'Miller'],
                    ];

 $data['rosters'][] = [
                        ['sport' => 'nfl', 'firstname' => 'Carson', 'lastname' => 'Palmer'],
                        ['sport' => 'nfl', 'firstname' => 'David', 'lastname' => 'Johnson'],
                    ];

 echo json_encode($data);

That should give you:

{
    "rosters": [
        [{
            "sport": "nfl",
            "firstname": "Tyrod",
            "lastname": "Taylor"
        }, {
            "sport": "nfl",
            "firstname": "Lamar",
            "lastname": "Miller"
        }],
        [{
            "sport": "nfl",
            "firstname": "Carson",
            "lastname": "Palmer"
        }, {
            "sport": "nfl",
            "firstname": "David",
            "lastname": "Johnson"
        }]
    ]
}

Here's an example of how you could use a loop to build this data structure.

$data = [];

// ...for the example, but you'll want this to be your source data.
$number_of_rosters = 2;
$number_of_players = 2;

foreach (range(1, $number_of_rosters) as $index => $roster) {
    foreach (range(1, $number_of_players) as $player) {
        $data['rosters'][$index][] = ['sport' => 'nfl', 'firstname' => 'Tyrod', 'lastname' => 'Taylor'];
    }
}

Upvotes: 1

Alaa M. Jaddou
Alaa M. Jaddou

Reputation: 1189

i think you need to create insted for loop to create sub array

for ($y = 1; $y < 3; $y++) {
    $players = array();
    for ( $x= 1; $x < 2; $x++ ) {
        $players["player".$x]['sport'] = "NFL";
        $players["player".$x]['firstname'] = "Tyrod";
        $players["player".$x]['lastname'] = "Taylor";

    }
    $roster['roster'.$y][] = $players;
}
// this will give you what you want my friend
echo json_encode($roster);

Upvotes: 2

Related Questions