I am trying to decode JSON data to PHP then output it to the site. If I have the following:
{
"name": "josh",
"type": "human"
{
I can do this (within PHP), to display or output my type:
$file = "path";
$json = json_decode($file);
echo $json["type"]; //human
So, if I have the following:
{
"name": "josh",
"type": "human",
"friends": [
{
"name": "ben",
"type": "robot"
},
{
"name": "tom",
"type": "alien"
}
],
"img": "img/path"
}
How can I output what type my friend ben is?
Use a loop like foreach and do something like the following:
//specify the name of the friend like this:
$name = "ben";
$friends = $json["friends"];
//loop through the array of friends;
foreach($friends as $friend) {
if ($friend["name"] == $name) echo $friend["type"];
}
To get the decoded data in array format you would supply true as the second argument to json_decode otherwise it will use the default which is object notation. You could easily create a function to shorten the process when you need to find a specific user
$data='{
"name": "josh",
"type": "human",
"friends": [
{
"name": "ben",
"type": "robot"
},
{
"name": "tom",
"type": "alien"
}
],
"img": "img/path"
}';
$json=json_decode($data);
$friends=$json->friends;
foreach( $friends as $friend ){
if( $friend->name=='ben' )echo $friend->type;
}
function finduser($obj,$name){
foreach( $obj as $friend ){
if( $friend->name==$name )return $friend->type;
}
}
echo 'Tom is a '.finduser($friends,'tom');
try this,
$friend_name = "ben";
$json=json_decode($data);
$friends=$json->friends;
foreach( $friends as $val){
if($friend_name == $val->name)
{
echo "name = ".$val->name;
echo "type = ".$val->type;
}
}
DEMO
Related
I'm looking to order data from a Json file with PHP :
$url = 'data.json';
$json_string = file_get_contents($url);
$json = json_decode($json_string);
$rows = $json->values;
foreach($rows as $row) {
$jsonf .= json_encode(array(
"$row[0]" => array(
"name" => "$row[1]",
"value" => "$row[2]",
"conso" => "$row[3]",
"ml" => "$row[4]"
)
), JSON_UNESCAPED_UNICODE);
}
Unfortunately, with this method, I get a bad result:
{
"DATA_01": {
"name": "name_01",
"value": "value_01",
"conso": "conso_01",
"ml": "ml_01"
}
} {
"DATA_02": {
"name": "name_02",
"value": "value_02",
"conso": "conso_02",
"ml": "ml_02"
}
} {
"DATA_03": {
"name": "name_03",
"value": "value_03",
"conso": "conso_03",
"ml": "ml_03"
}
}
I would rather get something like this:
{
"DATA_01": {
"name": "name_01",
"value": "value_01",
"conso": "conso_01",
"ml": "ml_01"
},
"DATA_02": {
"name": "name_02",
"value": "value_02",
"conso": "conso_02",
"ml": "ml_02"
},
"DATA_03": {
"name": "name_03",
"value": "value_03",
"conso": "conso_03",
"ml": "ml_03"
}
}
Could someone help me solve my problem.
Thank you very much for all the help you can give me.
I am not really sure what you want to accomplish, but I think it is the following:
You get this result because you are putting the json_encode function within a for-loop. put all your detain an array, and encode it after that. Your code will become like this:
foreach($rows as $row) {
$data[] = $row[0];
}
json_encode($data)
I'm having some problems generating graphs with drilldown in Highcharts.
I'm using Highcharts to render a pie with drilldown series.
I need to transform this output json
Drilldownseries = [
{
"name": "LAZIO",
"data": [["ROMA", 28]],
"id": "LAZIO"
},
{
"name": "LAZIO",
"data": [["FROSINONE", 218]],
"id": "LAZIO"
},
{
"name": "LAZIO",
"data": [["LATINA", 212]],
"id": "LAZIO"
},
{
"name": "TOSCANA",
"data": [["FIRENZE", 2]],
"id": "TOSCANA"
},
{
"name": "TOSCANA",
"data": [["LIVORNO", 5]],
"id": "TOSCANA"
},
{
"name": "TOSCANA",
"data": [["PISA", 9]],
"id": "TOSCANA"
}
];
to
Drilldownseries = [
{
"name": "LAZIO",
"data": [["ROMA", 28], ["FROSINONE", 218], ["LATINA", 212]],
"id": "LAZIO"
},
{
"name": "TOSCANA",
"data": [["FIRENZE", 2], ["LIVORNO", 5], ["PISA", 9]],
"id": "TOSCANA"
}
];
This is the part of query that populates the array:
...
if($res3)
{
$i = 0;
while ($row = mysqli_fetch_array($res3, MYSQLI_ASSOC))
{
$row3[$i]["name"] = $row["name"];
$row3[$i]["data"] = [[$row["subname"],$row["data"]]];
$row3[$i]["id"] = $row["id"];
$i++;
};
$row3 = json_encode($row3,JSON_NUMERIC_CHECK);
};
I'd prefer to extract the array with php well formed, but should be the same tranform the json.
PHP 7.2
Highcharts 6.1.1
for benefit of all, this is my solution. Code refined should be appreciated :)
First removed square brackets in code below
$row3[$i]["data"] = [$row["subname"],$row["data"]];
then create the new array so
$repl = array();
$i = 0;
foreach ($row3 as $value) {
if (!isset($repl[$i]['id'])) {
$repl[$i]['id'] = $value['id'];
$repl[$i]['name'] = $value['name'];
$repl[$i]['data'] = [$value['data']];
} elseif (($repl[$i]['id']) <>$value['id']) {
$i++;
$repl[$i]['id'] = $value['id'];
$repl[$i]['name'] = $value['name'];
$repl[$i]['data'] = [$value['data']];
} else {
array_push($repl[$i]['data'], $value['data']);
}
}
$resultx = json_encode($repl,JSON_NUMERIC_CHECK);
thanks
This is the JSON
{
"circuit_list": [
{
"_id": "58c0f378a986f808cdaf94cf",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
},
{
"_id": "58c0f378a986f808cdaf94d0",
"aggregation": {
"dev_name": "ME2-D2-BOO",
"port": {
"desc": "AKSES_SITE_SITE-TSEL_ME2-D2-BOO#1/2/5_200M_BOO082#CIPAKUBOO534",
"name": "1/2/5"
}
},
"area": "AREA 2",
"site_id": "N/A",
"site_name": "N/A"
}
}
I already try with this code
$json = json_decode($url, true);
foreach($json as $value)
{
$_id = $value->_id;
}
it didn't work. Please help, I need to get the value to show them on the view. Did I do this wrong? this json is difficult because i didn't understand the structure.
I usually decode json with format
[{"id":"1","name":"faisal"}]
like this and with my foreach it's working.
If the second parameter of json_decode is true, the function will return an array instead of an object. Also, you would need to loop over the circuit_list property of the object.
$json = json_decode($url); // <- remove the parameter
foreach($json->circuit_list as $value) // <- loop over circuit_list
{
$_id = $value->_id;
}
<?php
$json = json_decode($url,true);
foreach($json['circuit_list'] as $value)
{
$id = $value['_id'];
}
?>
I am accessing an API where the JSON data is returned as below.
{
"name": "",
"count": 4,
"frequency": "",
"version": 3,
"newdata": true,
"lastrunstatus": "success",
"thisversionstatus": "success",
"thisversionrun": "",
"results": {
"collection": [{
"textlink": {
"href": "http://text1.com",
"text": "Text 1"
},
"index": 1,
"url": ""
}, {
"textlink": {
"href": "http://text2.com",
"text": "Text 2"
},
"index": 2,
"url": ""
}, {
"textlink": {
"href": "http://text3.com",
"text": "Text 3"
},
"index": 3,
"url": ""
}, {
"textlink": {
"href": "http://text4.com",
"text": "Text 4"
},
"index": 4,
"url": ""
}]
}}
As you can see the JSON tree returned has multi-step levels. I am wanting to be able to take some of the deeper levels, in PHP, and insert into a database.
Currently I am using this code to try and echo the data (once I am able to I can then work on inserting it to a database no problem)
<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$results = json_decode($response, TRUE);
foreach($results as $item) {
echo $item->results[0]->collection[0]->textlink[0]->href;
echo "<br>";
echo $item->results->collection['href'];
echo "<br>";
echo $item->results->collection['text'];
}
?>
As you can see above I have tried several ways to access the deeper levels f data that are being displayed but with no avail.
I am currently getting errors of 'trying to get property of a non-object'. How is it possible to reach the data in this array?
try:
echo $results['results']['collection'][0]['textlink']['href'];
$obj = json_decode( $json, true ); foreach ( $obj['key'] as $key => $value ) { echo $value; }
foreach ($response['results']['collection'] as $textlink) {
$row = $textlink['textlink'];
echo $row['href'];
echo "<br>";
echo $row['text'];
echo "<br>";
}
you can do foreach like this, which loops only items in collection
I would suggest to do something like this (According to me, correct way to fetch results from API responses):
<?php
$request = "API.REQUEST.NET";
$response = file_get_contents($request);
$response = json_decode($response);
foreach($response->results->collection as $item) {
echo $item->textlink->href;
echo "<br>";
echo $item->textlink->text;
echo "<br>";
echo $item->index;
echo "<br>";
echo $item->url;
}
?>
I have a json file like this
{
"id": "123456789012345",
"members": {
"data": [
{
"name": "Dylan John",
"administrator": false,
"id": "12341234"
},
{
"name": "Test user",
"administrator": false,
"id": "43214321"
},
{
"name": "Demo user",
"administrator": false,
"id": "55445544"
},
{
"name": "Fake user",
"administrator": false,
"id": "11991199"
}
],
"paging": {
"next": "www.123456demo12345.com"
}
}
}
I need to extract the id of each name.
I just start my php code like that but simply display only the master id:
<?php
$url = "http://www.1234demo1234fake.com/user.json";
$json = file_get_contents($url);
$data = json_decode($json, TRUE);
echo $data[id]; //echo master id
echo $data[members][data][id];
?>
You have to iterate over $data['members']['data'] and print $data['members']['data'][$i]['id'].
Your JSON object contains property members again members has property data.
Now data is an array.
Just loop over data, you get array of objects (members), fetch property id from it.
<?php
$abc = json_decode($your_json_sting);
$membersData = $abc->members->data;
$memberIds = array();
foreach ($membersData as $mem) {
$memberIds[] = $mem->id;
}
echo '<pre>';print_r($memberIds);echo '</pre>';
?>