How to pick only 1 item within an array, JSON & PHP

Asked

Viewed 1,204 times

1

I’m learning a bit of json and php, I was able to display the values with the saveoffline api.

https://www.saveoffline.com/process/?url=https://drive.google.com/file/d/1S1mpK5wti6CEOeJSxJXcDphwWzwxEM7Y/view&type=json

on that link he returns to me:

{"title":"big_buck_bunny_720p_1mb.mp4","thumbnail":"https:\/\/lh5.googleusercontent.com\/KKqP0AeJV1nJ_hMFCoNCZT0O8ChZo1L_D7jFDW3NH3cleq8Af1gTt1cIDXE=w1200-h630-p","urls":[{"id":"https:\/\/www.saveoffline.com\/get\/?i=Ta4u04ottJ1pNuuBI0b825pX6NPCR7lo&u=9vry62JUyxM3dzmf0D9I90HlqKwN5g6S","label":"720p
- mp4","size":"x"},{"id":"https:\/\/www.saveoffline.com\/get\/?i=Ta4u04ottJ1pNuuBI0b825pX6NPCR7lo&u=GQe6sGKPBeZ0t7llfv3tkWnbdfLLFGiU","label":"480p
- mp4","size":"x"},{"id":"https:\/\/www.saveoffline.com\/get\/?i=Ta4u04ottJ1pNuuBI0b825pX6NPCR7lo&u=y5GcR5x21bLP7oqHebuniMyFygopsZIB","label":"360p
- mp4","size":"x"}]}

managed to display links etc. with foreach, how do I grab the link from the last line that contains 360p - mp4

{"id":"https:\/\/www.saveoffline.com\/get\/?i=Ta4u04ottJ1pNuuBI0b825pX6NPCR7lo&u=y5GcR5x21bLP7oqHebuniMyFygopsZIB","label":"360p
    - mp4","size":"x"}]}

would like to use this link to redirect to the video root link.

1 answer

1

You just need to select the index of the item you want. First create a variable to manipulate this JSON, if you have already done this, ignore.

$jsonS = json_decode($json_text);

I will use your JSON as an example.

"result":
[{
"id":"https:\/\/www.saveoffline.com\/get\/?i=Ta4u04ottJ1pNuuBI0b825pX6NPCR7lo&u=y5GcR5x21bLP7oqHebuniMyFygopsZIB",
"label":"360p - mp4",
"size":"x"
}]

In this return if you only want the link do the following:

echo "Link " . $jsonS->results[0]->id;

In response, the following is displayed::

Link https:\/\/www.saveoffline.com\/get\/?i=Ta4u04ottJ1pNuuBI0b825pX6NPCR7lo&u=y5GcR5x21bLP7oqHebuniMyFygopsZIB
  • in case I only want the link of the 3 option where is the quality "360p - MP4", but I fear that each video can have more than 4 options

  • I was able to pick up here, in case it is in $json->urls[2]->id position 2

  • Great! the path is the same, between the brackets you put the index the object you want to catch

Browser other questions tagged

You are not signed in. Login or sign up in order to post.