Loop over Array in array - php

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
}

Related

Retrieve videoId From JSON Using PHP

Link To JSON File: https://pastebin.com/gXcnNnUK
Code I've Tried So Far:
$json = json_decode($json, true);
foreach($json as $data){
echo $data['videoId'];
}
But it's not returning values. Is there any better approach to do it?
On line 3, did you mean to iterate over $arr instead of $json?
$json = file_get_contents('json.json');
$arr = json_decode($json, true);
foreach($arr['contents']['sectionListRenderer']['contents']['2']['itemSectionRenderer']['contents'] as $pero){
$video_id_list .= $pero['compactVideoRenderer']['videoId'].',';
}

get json value from php loop

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.

Reddit json and PHP - opening foreach() and getting values

I am trying to read a reddit json using my account as an example.
Tried the solution above as:
$string_reddit = file_get_contents("https://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);
$children = $json['data'];
foreach ($children as $child){
$linkkarma = $child['data']['link_karma'];
}
Also tried:
foreach ($json->data as $mydata){
$values["Latest_Karma"] = $mydata['link_karma'];
}
Also tried:
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"User-Agent: reddiant api script\r\n"
));
$context = stream_context_create($opts);
$url = "http://www.reddit.com/user/joshfolgado/about.json";
$json = file_get_contents($url, false, $context);
$result = json_decode($json, true);
foreach ($result as $child){
$values['Latest_Karma'] = $child['data']['link_karma'];
}
Spent a couple of hours trying do get the values for any of the items inside the "data" array, havent been able to get any.
What am I doing wrong? What am I missing?
Any help is appreciated.
Thanks
$string_reddit = file_get_contents("http://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);
$children = $json['data'];
foreach ($children as $child){
$link_karma= $child['link_karma'];
}
A slight modification to pee2pee's post (His returned an undefined index error for me)
$string_reddit = file_get_contents("http://www.reddit.com/user/joshfolgado/about.json");
$json = json_decode($string_reddit, true);
$children = $json['data'];
$user = [];
foreach ($children as $key => $value)
{
$user[$key] = $value;
}
echo $user['name']; //Now you can use the $user array to access all the properties!
This works for me ->

How to go through each element in JSON array in PHP

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;
}

PHP: Loop on JSON?

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();
...

Categories