2

I have used an api service from themoviedb.org, https://api.themoviedb.org/3/find/tt0986233?api_key=redacted&language=en-US&external_source=imdb_id

This API is presenting the data in a JSON ARRAY format. like this

{"movie_results":[{"adult":false,"backdrop_path":null,"genre_ids":[18,36],"id":10360,"original_language":"en","original_title":"Hunger","overview":"The story of Bobby Sands, the IRA member who led the 1981 hunger strike in which Republican prisoners tried to win political status. It dramatises events in the Maze prison in the six weeks prior to Sands’ death.","poster_path":"/84HdTM39G2MzyTl8N9R0wVU9I5b.jpg","release_date":"2008-05-15","title":"Hunger","video":false,"vote_average":7.3,"vote_count":655,"popularity":7.829}],"person_results":[],"tv_results":[],"tv_episode_results":[],"tv_season_results":[]}

Now I would like to get the values of original_title and overview in two php variable.
Note: I have already looked tons of code and didn't find any good results and my question was deemed as duplicate once and thus I would like to request anyone with enough skill to give me a solution.
Thank you very much in Advance.

Salman A
  • 262,204
  • 82
  • 430
  • 521
  • 1
    "_I would like to request anyone with enough skill to give me a solution_" Stack Overflow is *not* a code writing service though. We are always glad to help and support new coders but you need to help yourself first. You are expected to try to write the code yourself. Please read [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) and [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask). – brombeer Apr 28 '20 at 11:09
  • https://www.php.net/manual/en/function.json-decode.php – squareborg Apr 28 '20 at 11:13

1 Answers1

1

You can use json_decode() function to convert your JSON to Object or Array

<?php

$json = '{"movie_results":[{"adult":false,"backdrop_path":null,"genre_ids":[18,36],"id":10360,"original_language":"en","original_title":"Hunger","overview":"The story of Bobby Sands, the IRA member who led the 1981 hunger strike in which Republican prisoners tried to win political status. It dramatises events in the Maze prison in the six weeks prior to Sands’ death.","poster_path":"/84HdTM39G2MzyTl8N9R0wVU9I5b.jpg","release_date":"2008-05-15","title":"Hunger","video":false,"vote_average":7.3,"vote_count":655,"popularity":7.829}],"person_results":[],"tv_results":[],"tv_episode_results":[],"tv_season_results":[]}';

$arr = json_decode($json, TRUE);

print_r($arr['movie_results'][0]['original_language']);

Note that passing TRUE as a second arguments will convert the JSON object to associated Array

assoc: When TRUE, returned objects will be converted into associative arrays.

ROOT
  • 11,363
  • 5
  • 30
  • 45