1

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
asked Oct 5, 2015 at 19:12
0

1 Answer 1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.