Get last item URL - php

I have a data in following JSON sort of format in which I am trying to only get the very last URL which in my following example is https://lastexample.com/images/I/4162nAIaRCL.jpg . Is there an easy way I can retrieve this via PHP?
{"https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]}

Try, decode it into an array (json_decode) then get the keys (array_keys), then the last entry (end/max).
<?php
$json = '{
"https://example.com/images/I/4162nAIaRCL._SX425_.jpg": [425, 425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg": [355, 355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg": [466, 466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg": [450, 450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg": [500, 500]
}';
$array = json_decode($json, true);
$keys = array_keys($array);
echo end($keys); // https://lastexample.com/images/I/4162nAIaRCL.jpg
https://3v4l.org/MLbdt

Try this :
<?php
$json = '{
"https://example.com/images/I/4162nAIaRCL._SX425_.jpg":[425,425],
"https://example.com/images/I/4162nAIaRCL._SY355_.jpg":[355,355],
"https://example.com/images/I/4162nAIaRCL._SX466_.jpg":[466,466],
"https://example.com/images/I/4162nAIaRCL._SY450_.jpg":[450,450],
"https://lastexample.com/images/I/4162nAIaRCL.jpg":[500,500]
}';
$arr = json_decode($json,true);
$arrkeys = array_keys($arr);
$lastkey = end($arrkeys);
echo $lastkey;
?>

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'].',';
}

Using foreach Loop In PHP To Parse Json

I'm using iTunes RSS generator to get HOT tracks, now I'm using following way to parse JSON:
<?php
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
$cltn = $obj['feed']['results'][0]['collectionName'];
echo $cltn;
?>
Now, as we know that It'll return only 1 collectionName.
JSON request is returning 10 results. How can I get them all using foreach loop? I've used several ways but no success.
Since you didn't give the output of the array I assume the [0] index is what needs to be iterated.
You need to foreach the $obj['feed']['results'] by doing:
Foreach($obj['feed']['results'] as $cltn){
Echo $cltn['collectionName'];
}
Foreach over the results:
foreach ($obj['feed']['results'] as $result) {
echo $result['collectionName'] . '<br>' . PHP_EOL;
}
Based on the code you provided you can iterate the results to get all possible information from the artist/track list, for example:
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
$cltn = $obj['feed']['results'];
function test_print($item, $key)
{
echo "<strong>".$key."</strong>: ".$item."<br>";
}
foreach($cltn as $key => $c) {
echo "Result No ".($key+1)."<br>";
array_walk_recursive($c, 'test_print');
}
in case you only want to show artistName and collectionName you can slightly modify the above example:
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$obj = json_decode($jsondata,true);
$cltn = $obj['feed']['results'];
foreach($cltn as $c) {
echo $c['artistName'].": ".$c['collectionName']."<br>";
}
You can try all the above in PHP Fiddle
Try like this way with foreach() to iterate your array in key=>value manner as you've decoded the json as an array not an object in php.
<?php
$json_string = 'https://rss.itunes.apple.com/api/v1/in/apple-music/hot-tracks/all/10/explicit.json';
$jsondata = file_get_contents($json_string);
$array = json_decode($jsondata,true);
# printing resulted array just for debugging purpose
print '<pre>';
print_r($array);
print '</pre>';
foreach($array['feed']['results'] as $key=>$value){
echo $value['collectionName'].'<br/>';
}
?>

splite value array ini php

i really need help about arrays. I have a case like this
[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]
how in php code it can split become 2 category like this
$animal = [ "lion","rabbit","rat","mouse","horse","dog","cat"];
and
$car = ["honda","BMW","Toyota","Suzuki","mercedes","Jaguar","Audi"];
I really need to solve this. please help me
Json_decode the string and use array_column to get the columns in each variable.
$json = '[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]';
$arr = json_decode($json,1);
$animals = array_column($arr, "animal");
$cars = array_column($arr, "car");
var_dump($cars, $animals);
https://3v4l.org/NtL9e
Real easy. Decode the json, create two blank arrays, loop through, and add to each array:
<?php
$x = '[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]';
$y = json_decode($x, true);
$animals = [];
$cars = [];
foreach ($y as $z) {
$animals[] = $z['animal'];
$cars[] = $z['car'];
}
var_dump($animals);
var_dump($cars);
https://3v4l.org/Fb7Bq
try that:
$arr=json_decode('[
{"animal":"lion", "car":"honda"},
{"animal":"rabbit","car":"BMW"},
{"animal":"rat", "car":"Toyota"},
{"animal":"mouse", "car":"Suzuki"},
{"animal":"horse", "car":"mercedes"},
{"animal":"dog", "car":"Jaguar"},
{"animal":"cat", "car":"Audi"}
]');
var_dump($arr);echo'<br>';echo'<br>';
$animal=array();
$car=array();
foreach ($arr as $row){
$animal[]=$row->animal;
$car[]=$row->car;
}
var_dump($car);
var_dump($animal);

i cant get json array value using loop for (PHP)

i have 1 json but i cant get a value using for loop
$string = [{"lt":"1","lot":["1","1","1","1","1"]},{"lt":"2","lot":["0","0","0","0","0"]},{"lt":"3","lot":["0","0","0","0","0","0"]}]
$json = json_decode($string,true);
for($i = 0;$i<count($json);$i++){
for($j = 0;$j<count($json->lot);$i++){
if($json->lot==0){
echo $j;
}
}
}
i got this error : Trying to get property of non-object
edit your code :
$json = [{"lt":"1","lot":["1","1","1","1","1"]},{"lt":"2","lot":["0","0","0","0","0"]},{"lt":"3","lot":["0","0","0","0","0","0"]}]
$json = json_decode($json);
for($i = 0;$i<count($json);$i++){
for($j = 0;$j<count($json->lot);$i++){
if($json->lot==0){
echo $j;
}
}
changes json to decode in array using php json_decode(). json_decode
You need to use json_decode on your json. Also, keep in mind that json have to be stored as string in PHP.
<?php
$json = ['{"lt":"1","lot":["1","1","1","1","1"]}','{"lt":"2","lot":["0","0","0","0","0"]}','{"lt":"3","lot":["0","0","0","0","0","0"]}'];
foreach($json as $data) {
$current = json_decode($data);
foreach($current->lot as $lot)
echo $lot;
}
Used json_decode() function for decode json :
<?php
$json = '[{"lt":"1","lot":["1","1","1","1","1"]},{"lt":"2","lot":["0","0","0","0","0"]},{"lt":"3","lot":["0","0","0","0","0","0"]}]';
$json = json_decode($json);
foreach($json as $val){
foreach($val->lot as $lot) {
if($lot==0){
echo $lot;
}
}
}
?>

Get JSON from URL by PHP

I have a URL that returns a JSON object like this:
[
{
"idIMDB": "tt0111161",
"ranking": 1,
"rating": "9.2",
"title": "The Shawshank Redemption",
"urlPoster": "http:\/\/ia.media-imdb.com\/images\/M\/MV5BODU4MjU4NjIwNl5BMl5BanBnXkFtZTgwMDU2MjEyMDE#._V1_UX34_CR0,0,34,50_AL_.jpg",
"year": "1994"
}
]
URL : http://www.myapifilms.com/imdb/top
I want to get all of the urlPoster value and set in a array's element, and convert array to JSON so echo it.
How can I do it through PHP?
You can do something like that :
<?php
$json_url = "http://www.myapifilms.com/imdb/top";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
$json = file_get_contents('http://www.myapifilms.com/imdb/top');
$array = json_decode($json);
$urlPoster=array();
foreach ($array as $value) {
$urlPoster[]=$value->urlPoster;
}
print_r($urlPoster);
You can simply decode the json and then pick whatever you need:
<?php
$input = '[
{
"idIMDB": "tt0111161",
"ranking": 1,
"rating": "9.2",
"title": "The Shawshank Redemption",
"urlPoster": "http:\/\/ia.media-imdb.com\/images\/M\/MV5BODU4MjU4NjIwNl5BMl5BanBnXkFtZTgwMDU2MjEyMDE#._V1_UX34_CR0,0,34,50_AL_.jpg",
"year": "1994"
}
]';
$content = json_decode($input);
$urlPoster = $content[0]->urlPoster;
echo $urlPoster;
The output obviously is the URL stored in that property:
http://ia.media-imdb.com/images/M/MV5BODU4MjU4NjIwNl5BMl5BanBnXkFtZTgwMDU2MjEyMDE#._V1_UX34_CR0,0,34,50_AL_.jpg
BTW: "The Shawshank Redemption" is one of the best films ever made...
This how you do the same thing with array_map function.
<?php
#function to process the input
function process_input($data)
{
return $data->urlPoster;
}
#input url
$url = 'http://www.myapifilms.com/imdb/top';
#get the data
$json = file_get_contents($url);
#convert to php array
$php_array = json_decode($json);
#process the data and get output
$output = array_map("process_input", $php_array);
#convert the output to json array and print it
echo json_encode($output);

Categories