I have data in this format from an API:
{
"key1": 2300,
"key2": 152,
"key3": 5,
"key4": 18,
"key5": "value",
"players": [
{
"avatar": {
"name": "name",
"id": 73019554141,
"key": 34361224375
}]
"players": [
{
"avatar": {
"name": "name",
"id": 73019554141,
"key": 34361224375
}]
}
I'm using $jsondata = file_get_contents($url); //get the content from the url
and $data = json_decode($jsondata, true);
to retrieve the data. I can access the data using:
$key1= $data['key1'];
$key2= $data['key2'];
How do I get the player values?
user812786
4,4306 gold badges42 silver badges50 bronze badges
1 Answer 1
Your JSON is not valid. Assuming the below JSON, it would be as follows:
$jsondata='{
"key1": 2300,
"key2": 152,
"key3": 5,
"key4": 18,
"key5": "value",
"players": [
{
"avatar": {
"name": "name",
"id": 73019554141,
"key": 34361224375
}
}
]
}';
$data=json_decode($jsondata);
echo($data->players[0]->avatar->name.' '.$data->players[0]->avatar->id.' '.$data->players[0]->avatar->key);
http://sandbox.onlinephpfunctions.com/code/6f9d8227def5042d2c7280816c39fc9edc514263
answered Oct 5, 2015 at 19:24
lang-php