I want to echo out each element of an object of a JSON array like this:
{"request_list":[{"id":"1","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null},{"id":"3","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null}]}
But i cannot do it.I tried somethinglikethis:
$object = json_decode($json, true);
$request_list = $object->request_list;
foreach($request_list as $r){
echo $r->name;
echo $r->blood_type;
echo $r->phone_number;
}
But I got an error like:
Invalid argument supplied for foreach()
As you have mark return as array true in json_decode. So, try below code.
$object = json_decode($json, true);
$request_list = $object['request_list'];
foreach($request_list as $r){
echo $r['name'];
echo $r['blood_type'];
echo $r['phone_number'];
}
Use this
$object = json_decode($json, true);
$request_list = $object['request_list'];
foreach($request_list as $r){
echo $r['name'];
echo $r['blood_type'];
echo $r['phone_number'];
}
try
$json = '{"request_list":[{"id":"1","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null},{"id":"3","name":"yunus","surname":"smsk","phone_number":"05350601922","blood_type":"0","unit_of_blood":"0","date":null}]}';
$data = json_decode($json );
$request = $data->request_list;
foreach($request as $request_data){
echo $request_data->id;
echo $request_data->name;
echo $request_data->surname;
}
Related
I've got this data: http://api.tvmaze.com/shows?page=0
What i am trying to achieve is looping over the data and echo each id. This is the code i used:
<?php
$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
foreach( $response as $serie){
foreach( $serie as $info => $value){
echo $info->$value["id"];
}
}
I don't really know what i am doing wrong.. Do you guys have any idea?
Greetz,
Try below code for getting id and url. You have to use array because you are passing TRUE in json_decode().
<?php
$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
foreach( $response as $serie)
{
echo $serie['id']."->".$serie['url']."<br>";
}
?>
Keep it simple to fetch ids you wanted,
$url = ('http://api.tvmaze.com/shows?page=0');
$json = file_get_contents($url);
$response = json_decode($json, TRUE);
echo "<pre>";
foreach ($response as $value) {
echo $value['id']."<br/>"; // you will get ids in here only
}
I have this json "https://www.decodethis.com/webservices/decodes/1Fmzu64p5yzb76700/aYcBzLfMCRs_JScq_sZt/0.json"
and I decode it with $data = json_decode($json, true);
But when I try to print the Vehicle part foreach($data['vehicle'][0] as $p)
{echo $p;}
the system prints"Invalid argument supplied for foreach()" and "Undefined index".
I'm a newbie, so forgive me.
Edit:This worked very well foreach ($data['decode']['vehicle'][0] as $p) echo $p;, but now I wonder if I can know the key or index of the element $p on every loop, as example "body>driveline>engine>id"...
the path is like these;
foreach ($data['decode']['vehicle'][0] as $p) echo $p;
You may want to to use:
$json = file_get_contents("https://www.decodethis.com/webservices/decodes/1Fmzu64p5yzb76700/aYcBzLfMCRs_JScq_sZt/0.json");
$obj = json_decode($json, true);
foreach($obj["decode"]["vehicle"] as $vehicle )
{
echo $vehicle['body'];
echo $vehicle['engine'];
# etc...
}
For accessing output in json file there is decode as root key. So first access decode key then after use vehicle information.
Below is code for that:
$data = json_decode($json, true);
$json = $data["decode"]["vehicle"][0];
foreach($json->entries as $row) {
foreach($row as $key => $val) {
echo $key . ': ' . $val;
echo '<br>';
}
}
how can I get the same results/values in one loop rather than two?
$json = file_get_contents($url)
$data = json_decode($json, true);
$desc = $data["descriptions"];
$assets = $data["assets"];
foreach ($assets as $assItem) {
echo $assItem["assetid"];
}
foreach($desc as $descItem) {
echo descItem["name"];
}
I've tried something like
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data as $item) {
echo $item["assets"]["assetid"];
echo $item["descriptions"]["name"];
}
pastebin to the json: https://pastebin.com/raw/uA9mvE2e
You could do something like:
$json = file_get_contents($url);
$data = json_decode($json, true);
foreach ($data['assets'] as $k => $item) {
echo $item["assetid"];
echo $data["descriptions"][$k]["name"];
}
This assumes that $data['assets'] and $data['descriptions'] share the same indices.
i have the following JSON:
{"Switches":["Auswahl1","Auswahl2","Auswahl3"],"Check_MK":["Auswahl1","Auswahl2","Auswahl3"],"Testgroup":["Auswahl1","Auswahl2","Auswahl3"],"Printer":["Auswahl1","Auswahl2","Auswahl3"],"CAD":["Auswahl1","Auswahl2","Auswahl3"]}
How do i loop each object while using PHP?
My thoughts were the following:
<?php
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
foreach($decode as $key => $value) {
echo $key . $value;
}
?>
Doesn't work..... Also
echo $decode[1];
and
echo $decode[1][1];
doesn't work..
You need to add a second parameter to json_decode()
This parameter returns associative array instead of existing object (if existing).
$decode = json_decode($jsonfile, TRUE);
This will convert your JSON decoded data into associative array.
$jsonfile = file_get_contents('tags.json');
echo $jsonfile . "<br><br>";
$decode = json_decode($jsonfile);
now $decode is equivalent to:
$decode = new stdClass();
$decode->Switches = array();
$decode->Switches[] = "Auswahl1";
$decode->Switches[] = "Auswahl2";
$decode->Switches[] = "Auswahl3";
$decode->Check_MK = array();
...
I have a json file that I want to read with PHP
http://rh.ernestek.cz.cc/webeast/static.txt
I wanted to extract the "id" and the "key"
I wanted to loop through the id and key so that I can produce a table like this:
ID Key
1 9d07d681e0c1e294264724f7726e6f6f29
3 9cd1e3a4a04b5208862c3140046f73b515
...
I tried to extract the first id and key out using the following code but no luck:
<?php
$json = file_get_contents('static.txt');
$json_decoded = json_decode($json);
echo "ID: ".$json_decoded->static->1->id."<br />";
echo "Key: ".$json_decoded->static->1->key."<br />";
?>
Is there anything I an wrong?
Any ideas?
Thanks!
Decode the json as an array (pass true as second parameter), and loop over the array like so
<?php
$json = file_get_contents('static.txt');
$json_decoded = json_decode($json, true);
foreach ($json_decoded['static'] as $item) {
echo 'ID: ', $item['id'], '<br/>';
echo 'Key: ', $item['key'], '<br/>';
}
By using true in json_decode() it will convert all objects to array:
$json = file_get_contents("http://rh.ernestek.cz.cc/webeast/static.txt");
$json_decoded = json_decode($json, true); // return array
$table = array();
foreach($json_decoded["static"] as $array){
$table[$array["id"]] = $array["key"];
}
//creating the table for output
$output = '<table border="1"><tr><td>ID</td><td>Key</td></tr>';
foreach($table as $id => $key){
$output .= "<tr><td>$id</td><td>$key</td></tr>";
}
$output .= '</table>';
echo $output;