Reputation: 663
I just started working with JSON, so this is a hard way for me. I spent more than 15 hours trying to configure out how I should create and fill an array in PHP to get specified JSON format.
I need to get exactly this example1
{"players":["xZero","Lux"],"points":[{"player":"xZero","points":"20"}]}
and this example2
{"players":["xZero","Lux"],"points":[]}
PHP will use foreach loops to fill data in example1 from database.
foreach($userdb as $users){
$output["users"][$i]=$users;
$i++;
}
This is just first half, I do not have any idea how to insert data in another half.
To get exact output like example1.
For example 2 I also don't have idea how to fill it out. I known for first half with users, but another half is mistery, like in example1.
As you can see, my experience with JSON is extremely poor. Also I'm not verry experienced with multidimensional arrays.
Thanks in advance for any help!
Upvotes: 1
Views: 75
Reputation: 6922
Based on your JSON your PHP array should look something like this:
array("players"=>array("xZero","Lux"),"points"=>array(array("player"=>"xZero","points"=>20)))
My best advice for you is to populate an array like this or create objects using classes and then using the json_encode()
function.
Upvotes: 1