Accessing JSON array after json_decode/multidimensional array
Assuming that you've chosen to decode the JSON as a multi-dimensional array, rather than as objects:
foreach ($results as $tweet) {
$user = $tweet["from-user"];
$text = $tweet["text"];
$entities = $tweet["enities"];
$urls = $entities["urls"];
foreach ($urls as $url) {
echo $url["expanded_url"];
}
}
et cetera
How to access object inside multidimensional array with json decode in PHP?
My guess is that, there are multiple images, not sure which one would be desired, it could be any of these:
$variant['files'][1]['preview_url']
or
$variant['files'][0]['preview_url']
Test
$variants = json_decode($variants, true);
$html = '';
foreach ($variants['result']["sync_variants"] as $variant) {
$html .= '<img src="' . $variant['files'][1]['preview_url'] . '"><br>';
}
echo $html;
Output
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
<img src="https://d1yg28hrivmbqm.cloudfront.net/files/fa1/fa1a4b2f561f28529f4230ba49eca1db_preview.png"><br>
PHP Json_decode Multidimensional Array
If you can access the images key, then:
<?php
$json = <<<JSON
{
"title":"A Title Here",
"images":[
{
"coverType":"fanart",
"url":"some_random_file_here.jpg"
},
{
"coverType":"banner",
"url":"another_random_file_here.jpg"
},
{
"coverType":"poster",
"url":"yet_another_random_file_here.jpg"
}
]
}
JSON;
$json = json_decode($json);
print_r($json);
foreach ($json->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
?>
Gives:
Image Cover Type: banner
URL: another_random_file_here.jpg
UPDATE:
The JSON file you link to seems invalid, missing braces after each series. Here's the corrected JSON, and code:
<?php
$json = <<<JSON
[
{
"series": {
"title": "Brooklyn Nine-Nine",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/269586-15.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/269586-g3.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/269586-13.jpg"
}
],
"year": 2013
}
},
{
"series": {
"title": "The Middle",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/95021-16.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/95021-g14.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/95021-8.jpg"
}
],
"year": 2009
}
},
{
"series": {
"title": "New Girl",
"images": [
{
"coverType": "fanart",
"url": "http://thetvdb.com/banners/fanart/original/248682-43.jpg"
},
{
"coverType": "banner",
"url": "http://thetvdb.com/banners/graphical/248682-g20.jpg"
},
{
"coverType": "poster",
"url": "http://thetvdb.com/banners/posters/248682-14.jpg"
}
],
"year": 2011
}
}
]
JSON;
$json = json_decode($json);
// echo '<pre>' .print_r($json, 1) .'</pre>';
foreach ($json as $item)
{
echo 'Title: ' .$item->series->title .'<br/>';
foreach ($item->series->images as $img)
{
if ( $img->coverType == "banner" )
{
echo 'Image Cover Type: ' .$img->coverType .'<br/>';
echo 'URL: ' .$img->url .'<br/>';
}
}
}
?>
Gives:
Title: Brooklyn Nine-Nine
Image Cover Type: banner
URL: http://thetvdb.com/banners/graphical/269586-g3.jpg
Title: The Middle
Image Cover Type: banner
URL: http://thetvdb.com/banners/graphical/95021-g14.jpg
Title: New Girl
Image Cover Type: banner
URL: http://thetvdb.com/banners/graphical/248682-g20.jpg
decode json multidimensional array in PHP
I assume you have decoded the JSON text with $json = json_decode($response);
. It is a bit awkward to call the result of that "JSON". It is the response you got which is JSON, but once you decode it, it is a PHP object (with nested array properties, which again have object properties, ...etc); that's not JSON.
Now to your code. The outer loop is fine:
foreach ($json->data as $flight_list)
But the next loop is wrong. It ignores the fact that offerItems
, services
, segments
are all (indexed) arrays, so you cannot do offerItems->something
. Instead, you should iterate those arrays:
foreach ($json->data as $flight_list) {
foreach ($flight_list->offerItems as $offerItem) {
foreach($offerItem->services as $service) {
foreach($service->segments as $segment) {
// Uncomment next line to see what you have at this level:
//echo json_encode($segment) . "\n";
// Example of what you could get and output:
echo "from {$segment->flightSegment->departure->iataCode} to {$segment->flightSegment->arrival->iataCode}\n";
}
}
}
}
decode multidimensional json array in php
This will echo them out and add to array. I wanted to show you another option :)
$arr = json_decode($json,true);
$banners = array();
$num = count($arr["acf"]["hero_banner"]);
for($x = 0; $x < $num; $x++){
echo $img = $arr["acf"]["hero_banner"][$x]["banner_image"] . " ";
$banners[] = $img;
}
print_r($banners);
Echo Multidimensional json and php
Firstly, you need to use json_decode
only once, and your keys are having spaces and the keys which are having spaces then you need to enclose it as string and in {}
. Try it like,
$datakota = json_decode(file_get_contents($varkota, true));
$data1 = $datakota[0]->{'Value 2'};
$data2 = $data1->{'Value 3'}; // no need to use array [0], as Value 2 is object
echo $data2->Value4; // no need to use array [0], as Value 3 is also an object
In a single line, just use it as
echo $datakota[0]->{'Value 2'}->{'Value 3'}->Value4;
Online Demo
Related Topics
How to Use String Concatenation to Define a Class Const in PHP
Error: File Is Encrypted or Is Not a Database
Updating Product Stock Programmatically in Woocommerce 3
PHP - Returning the Last Line in a File
How to Correct the [Composer\Downloader\Transportexception] Error for Composer
Using Simplexml to Load Remote Url
Capture Newline from a Textarea Input
Php: Split Multibyte String (Word) into Separate Characters
Composer Cannot Download Files
PHP Recursively Unset Array Keys If Match
File Attachment with PHPmailer
Is There Any Sort of "Pre Login" Event or Similar
PHP and Microsoft Access Database - Connection and Crud