Search code examples
phphtmljsonapithemoviedb-api

Parse specific part out of JSON


I'm currently working on a german movie-related website. As i'm new to coding, i've bought a script that parses movie data from themoviedb.

Here is the full JSON response that is being parsed:

https://api.themoviedb.org/3/movie/283995?api_key=9ff892d334d361aac74e06ac0e1acb96&append_to_response=releases

A little research on the script shows that the standard release_date is parsed by:

public function getReleaseDate()
{
    return (array_key_exists('release_date', $this->raw) ? $this->raw['release_date'] : $this->raw['first_air_date']);
}

This works fine and gives me "2017-04-25". But inside that JSON response there are region-specific release-dates i want to filter out. I specifically want only the "release_date" from this part here:

{
certification: "",
iso_3166_1: "DE",
primary: false,
release_date: "2017-04-27"
},

TL;DR: I need a function that parses the JSON response in a way that i receive only the "release_date" of the german release ("2017-04-27").

Here is what i tried:

 public function getReleaseDateLocalized()
{
    $date = '';
    if (isset($this->raw['releases']['countries']))
    {
        $dates = $this->raw['releases']['countries'];
    }
    elseif(isset($this->raw['release_date']))
    {
        return $this->raw['release_date'];
    }
    else
    {
        return '0000-00-00';
    }
    foreach($this->raw[$dates] as $locdates)
    {
        if (is_array($locdates))
        {
            $de .= array_search("DE", $locdates);
            $date .= raw[$de]['release_date'];

        }
        else
        {
            $date .= $this->raw['release_date'];                
        }       
    }
    return $date;
}

Solution

  • public function getReleaseDate()
    {
      $countries = $this->raw['releases']['countries'];
      $de = array_search('DE', array_column($countries, 'iso_3166_1'));
    
      return $countries[$de]['release_date'];
    }