0

I'm Having issues grabbing values in a multi level array.

This is my JSON I am grabbing with CURL and putting into a variable.

{
  "id": 454626,
  "results": [
    {
      "iso_3166_1": "SK",
      "release_dates": [
        {
          "certification": "U",
          "iso_639_1": "sk",
          "note": "",
          "release_date": "2020-02-20T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "DE",
      "release_dates": [
        {
          "certification": "6",
          "iso_639_1": "",
          "note": "",
          "release_date": "2020-02-13T00:00:00.000Z",
          "type": 3
        }
      ]
    },
    {
      "iso_3166_1": "TW",
      "release_dates": [
        {
          "certification": "G",
          "iso_639_1": "",
          "note": "",
          "release_date": "2020-02-21T00:00:00.000Z",
          "type": 3
        }
      ]
    }
  ]
}

This is my PHP code that I'm having issues with.

$id_tmdb = $row[id_tmdb];

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.themoviedb.org/3/movie/$id_tmdb/release_dates?api_key=API-KEY",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET"
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

$result = json_decode($response, true);


foreach($result[results][release_dates] as $key=>$val){
    echo "$val[release_date]";
}

I'm trying to get the release_date but it just shows blank.

I have another page that is formatted the same way so I am unsure why it isn't working.... Thank you for all your help.

5
  • Do you have error reporting turned on? Also, did you copy/paste the code or did you write some of it by hand here? Commented Mar 8, 2020 at 21:39
  • copy and pasted it Commented Mar 8, 2020 at 21:52
  • Also error reporting is turned on and not reporting anything Commented Mar 8, 2020 at 21:53
  • That is weird as you should be seeing at least 4 warnings/notices. So, is your goal just getting all the release dates from all the results? Commented Mar 8, 2020 at 21:59
  • Correct need all the release dates Commented Mar 8, 2020 at 22:06

2 Answers 2

2

Your $result['results'] value is also an array and you need to iterate over that as well as the release_dates array:

foreach($result['results'] as $results) {
    foreach ($results['release_dates'] as $val){
        echo "$val[release_date]\n";
    }
}

Output (for the sample JSON provided):

2020-02-20T00:00:00.000Z
2020-02-13T00:00:00.000Z
2020-02-21T00:00:00.000Z

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhh yes that makes sense. Been hitting my head against the wall for hours. Thanks for your help
0

This should get you there:

foreach($result['results'] as $res){
    echo $res['release_dates'][0]['release_date']."\n";
}

3v4l link

On a side-note, I put the array keys in quotes to prevent php warnings.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.