Reputation: 37
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]}
Upvotes: 0
Views: 71
Reputation: 46602
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
Upvotes: 1
Reputation: 330
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;
?>
Upvotes: 1