I have a JSON file
{ "status": "ok", "data": { "files": { "4d7cc5ec-2aef-48ec-9ebf-f811d4c77a5d": { "mimetype": "video/mp4", "link": "https://example.com/video.mp4", "name": "video.mp4", "size": 348353594 } }, "uploadTime": 1618074643, "totalSize": 348353594 } }
Now the value 4d7cc5ec-2aef-48ec-9ebf-f811d4c77a5d
keeps changing every time the page is loaded. And I want to grab the value of link
My code is
//This one also not working. Gives 500 Internal Error. $jsonfile = 'this is JSON file'; $json = json_decode($jsonfile); $link = $json->data->files->4d7cc5ec-2aef-48ec-9ebf-f811d4c77a5d->link; echo $link;
But the problem is 4d7cc5ec-2aef-48ec-9ebf-f811d4c77a5d
keeps changing every time. How do I then get the value of link using PHP?
Advertisement
Answer
Use array_column()
as well as true
as a second parameter to json_decode()
$json = json_decode($jsonfile,true); $link = array_column($json['data']['files'],'link'); var_dump($link[0]);
sample output: https://3v4l.org/oSQvo
Note: In case files
contains multiple dynamic values then $link
will give you array of all links. you can apply foreach()
to iterate and get individual links