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?
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
Related
I am creating a json file from my array:
$file = json_encode($array);
The json file will look like this:
[
{
"name": "file1.html",
"date": "2019-01-29T20:33:57.000163Z",
"size": "348"
}
{
"name": "file2.xml",
"date": "2019-01-29T20:33:57.000167Z",
"size": "401"
}
{
"name": "file3.html",
"date": "2019-01-29T20:33:57.000171Z",
"size": "1314"
}
]
But I need to create a json file with some little bit different format. The output I need is:
{
"draw": 1,
"recordsTotal": 5000,
"recordsFiltered": 5000,
"data": [
{
"name": "file1.html",
"date": "2019-01-29T20:33:57.000163Z",
"size": "348"
}
{
"name": "file2.xml",
"date": "2019-01-29T20:33:57.000167Z",
"size": "401"
}
{
"name": "file3.html",
"date": "2019-01-29T20:33:57.000171Z",
"size": "1314"
}
]
}
Is this possible with json_encode?
Create a new array with rest of the info and assign current array data into it as well.
$newArray = array(
'draw'=> 1,
'recordsTotal'=> 5000,
'recordsFiltered'=> 5000,
'data'=>$array
);
$file = json_encode($newArray);
Im posting a multipart with text and files and trying to pass data to form, but these data are separated, so I want to combine them.
$request->request->all()
$request->files->all()
$form = $this->createForm(ParkingType::class, new Parking());
$form->submit($INeedToPassTheCombinedArray);
if ($form->isValid()) {
return $form->getData();
}
Both arrays have the same structure.
For example:
$request->request->all()
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [{
"total": 4,
"capacity": 928,
"places": [{
"total": 123,
"name": "test",
"address": "test"
},
{
"total": 123,
"name": "test",
"address": "test"
}
]
}]
}
$request->files->all()
{
"parkings": [{
"generalInfo": "File.pdf",
"places": [{
"logo": "File1.png"
},
{
"logo": "File2.png"
}
]
}]
}
I want to combine then in one single array, getting this:
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [{
"total": 4,
"capacity": 928,
"generalInfo": "File.pdf",
"places": [{
"total": 123,
"name": "test",
"address": "test",
"logo": "File1.png"
},
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File2.png"
}
]
}]
}
I tried using array_merge, but the result is a single array which contain 2 arrays. It is not adding the data of one array in the respective position of the other array.
I want to know if there is some method for do this automatically and elegant.
Hope this will solve your problem
$data1 = json_decode($data,true);// first post array
$data2 = json_decode($file,true);//second file array
foreach($data1['parkings'] as $key=>&$val){ // Loop though one array
$val2 = $data2['parkings'][$key]; // Get the values from the other array
$val += $val2; // combine 'em
foreach($val['places'] as $k=>&$v){
$val3 = $val2['places'][$k]; // Get the values from the other array
$v += $val3; // combine 'em
}
}
echo json_encode($data1);
{
"name": "Test",
"taxId": "asd12",
"nationality": "england",
"parkings": [
{
"total": 4,
"capacity": 928,
"places": [
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File1.png"
},
{
"total": 123,
"name": "test",
"address": "test",
"logo": "File2.png"
}
],
"generalInfo": "File.pdf"
}
]
}
I have the following json :-
{
"firstName": "Jhon",
"lastName": "Doe",
"username": "jhon",
"avatar": "localhost/uploads/avatars/default.jpg",
"language": "ar",
"birth_date": "2017-11-22 00:00:00",
"weight_chart": [],
"health_status": {
"id": 130,
"user_id": 258,
"weight": 95,
"height": 171,
},
I decoded the above json
$user = json_decode($response);
Now i am able to print the firstname by using:
$user->firstName
My questions is :-
Can i access the json values without decoding it ?
How can i access the "health_status" values id, weight ... ?
Your json is malformed, try this structure:
$str = '{
"firstName": "Jhon",
"lastName": "Doe",
"username": "jhon",
"avatar": "localhost/uploads/avatars/default.jpg",
"language": "ar",
"birth_date": "2017-11-22 00:00:00",
"weight_chart": [],
"health_status": {
"id": 130,
"user_id": 258,
"weight": 95,
"height": 171
}
}';
$obj = json_decode($str);
echo $obj->firstName.' - ';
echo $obj->health_status->id.' - ';
echo $obj->health_status->weight;
response: Jhon - 130 - 95
I am trying to get back information from a multidimensional json array but can't seem to get it right.
Below is an example of the output from the url which you can see yourself at http://109.255.189.130:3000/
{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 53462.0, "uptime_peak": 53462.0, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111752, "items_max": 112259, "items_peak": 112259, "mobiles": 37963, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }
What I would like to do is echo the name in the players array. There will be more than one player.
Can anyone please help me out here and point me in the correct direction to get this information?
I am very new to json so excuse my ignorance on the subject.
Thank you!
<?php
$json = '{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 54383.3, "uptime_peak": 54383.3, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111672, "items_max": 112259, "items_peak": 112259, "mobiles": 37944, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }';
$array = json_decode($json, true);
// you want 'true' as the second parameter so it tunrs this JSON data into a multidimensional array instead of objects.
foreach($array['players'] as $player){
print_r($player['info']);
//etc.. do what you want with each player
}
$json = '{ "vnc_version": "2.1.0.0", "mod_version": "1.0.0.0", "server": { "name": "Pure Blood", "framework": "Microsoft Windows NT 6.2.9200.0" }, "stats": { "uptime": 53462.0, "uptime_peak": 53462.0, "online": 1, "online_max": 1, "online_peak": 2, "unique": 1, "unique_max": 1, "unique_peak": 2, "items": 111752, "items_max": 112259, "items_peak": 112259, "mobiles": 37963, "mobiles_max": 37976, "mobiles_peak": 37978, "guilds": 0, "guilds_max": null, "guilds_peak": 0 }, "players": [ { "info": { "id": 1, "name": "aN.Droid", "title": "", "profile": "", "guild_id": -1, "guild_abbr": "" }, "stats": [ ], "skills": [ ], "equip": [ ] } ], "guilds": [ ] }';
$arr = json_decode($json, true);
foreach($arr["players"] as $player) print($player["info"]["name"]."<br/>");
I'm Getting values like this in JSON format -
{
"comments": [{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
}
But i don't want first array name means i need result like this -
[{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
Need a help...
Do like this...
<?php
$json='{
"comments": [{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]
}';
$arr=json_decode($json,1);
echo json_encode($arr['comments']);
OUTPUT :
[{"name":"ABC","desc":"Hello...","values":[{"status":"fine","label":""}]},{"name":"XYZ","desc":"Good Morning..","values":[{"status":"fine","label":"happy"}]}]
Try this:
$data = '[{
"name": "ABC",
"desc": "Hello...",
"values": [{
"status": "fine",
"label": ""
}]
}, {
"name": "XYZ",
"desc": "Good Morning..",
"values": [{
"status": "fine",
"label": "happy"
}]
}]';
$return = json_decode($data, true);
foreach ($return as $key => $value){
unset($return[$key]['name']);
}
echo '<pre>';
print_r($return);
echo '</pre>';