This is my json Output. i want to echo 'resolution' only. How is that possible?
Array
(
[uploader] => CoversDamian
[formats] => Array
(
[0] => Array
(
[preference] => -50
[resolution] => 720p
)
[1] => Array
(
[preference] => -100
[resolution] => 1080p
)
)
)
$myarray = Array
(
[uploader] => CoversDamian
[formats] => Array
(
[0] => Array
(
[preference] => -50
[resolution] => 720p
)
[1] => Array
(
[preference] => -100
[resolution] => 1080p
)
)
)
echo $myarray["formats"][1]["resolution"];
if you have more array in formats
key then you can use foreach
loop based on formats
key. Becuase you want to print all resolution under the formats
key. So
foreach($myarray["formats"] as $key => $value){
echo $value["resolution"]."<br>";
}
In the case you want to echo all resolutions you need a loop like this:
for ($i = 0; $i < sizeof($array["formats"]); $i++){
echo $array["formats"][$i]["resolution"];
}
Hope it helps!
Assuming the json is in object notation, stored as $json
you can loop through the 'formats' as follows :
foreach($json->formats as $key=>$value) {
echo $value->resolution . "
";
}
If it is not in object notation, you can loop through the sub-keys in the array (assuming it is store in the variable $json
) as follows :
foreach($json['formats'] as $key=>$value) {
echo $value['resolution'] . "
";
}
Notice the subtle difference in the way you can access sub-keys/elements in an object vs in an array.
$arr = array(
'uploader' => 'CoversDamian',
'formats' => array
(
0 => array
(
'preference' => '-50',
'resolution' => '720p'
),
1 => array
(
'preference' => -'100',
'resolution' => '1080p'
)
)
);
foreach ($arr['formats'] as $key=>$val) {
echo $val['resolution'];
}