1

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.

4
  • That isn't a JSON array, that's an object with other objects as properties. Commented Dec 18, 2015 at 22:54
  • Actually when i went to jsonformatter.curiousconcept.com thats what it outputed for me and said "VALID JSON (RFC 4627)" Commented Dec 18, 2015 at 22:58
  • 1
    it is a valid json, but note the difference between { } which defines a JSON object, and [ ] which defines a JSON array. I'm compiling a bit of code to achieve exactly what you need, unless the resulting JSON grimmdude replied with suits your needs. Commented Dec 18, 2015 at 23:04
  • I would love to see your response as well everything is helpful Commented Dec 18, 2015 at 23:12

2 Answers 2

2

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

Comments

1

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'];
    }
}

4 Comments

What I don't get with this is why does $data['rosters'][] just append if your setting it equal to it? i don't get that. Why doesn't it replace it like if you did $x = "1"; $x = "2"; then the final output of $x would be 2?
$array[] = 'new element' That syntax appends a new element to an array in PHP. That's just the way it goes!
the problem i have with your code example though is can you please present it in a for next loop to suggest data being dynamically pulled like Alaa did?
Sure, I added a loop example above. The problem is I don't know how you have your source data, so you'll need to twist some wires to suit it for your needs. This is just how I would do it.

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.