Search code examples
phpjsonforeachnested-loops

Pull data from nested json loop php


Ok, I have been fighting this for two days now!! Here is my Json structure. [

{
    "uuid":"/random number==",
    "meeting_number":7196503037,
    "host_id":"random number",
    "topic":"My's Personal Meeting Room",
    "start_time":"2015-01-06T22:01:07Z",
    "timezone":"America/Denver",
    "duration":56,
    "total_size":378080,
    "recording_count":1,
    "recording_files":[
        {
            "id":"long number",
            "meeting_id":"/NS90bsSTLeGzo6cT0nwXw==",
            "recording_start":"2015-01-06T22:01:09Z",
            "recording_end":"2015-01-06T22:01:14Z",
            "file_size":378080,
            "play_url":"https://myurl"
        }
    ]
},

I am trying to pull the Start Time from the top level array (which is named 'meetings') and the Play Url from the second level array (recording_files). I am using json_decode to decode the file into arrays.

I have tried many variations found on this site and nothing seems to get to the play url. VARIATION 1 $aha = json_decode($response,true);

        foreach ($aha['meetings'] as $key => $value){
            foreach($key['recording_files'] as $subkey => $newvalue){


        echo '<p>Time : '.$value['start_time'].'</p>
  <a href="'.$newvalue['play_url'].'">Recording</a><br>';

}} This pulls an invalid argument in foreach.

VARIATION 2 foreach ($aha['meetings'] as $key => $value){

        echo '<p>Time : '.$value['start_time'].'</p>
  <a href="'.$value['play_url'].'">Recording</a><br>';

} This pulls undefined index for play_url. As does the same code with the reference to ['recording_files'] placed before ['play_url']

VARIATION 3: Then I tried giving up on the first level and just pulling data from the second level: $aha = json_decode($response,true);

        foreach ($aha['meetings']['recording_files'] as $key => $value){


        echo '<p>Time : '.$value['recording_start'].'</p>
  <a href="'.$value['play_url'].'">Recording</a><br>';

} That gives me an undefined index on ['recording_files'] and a foreach error.

VARIATION 4. $aha = json_decode($response,true);

        foreach ($aha['meetings'] as $key => $value){
         $link=$key['recording_files'];

        echo '<p>Time : '.$value['start_time'].'</p>
  <a href="'.$link['play_url'].'">Recording</a><br>';

} This gets me the closest - no errors but no link. I presume that here the code is seeing the field but just not pulling the data from it...

Please help a newbie json person!!


Solution

  • Edit:

    If your data looks like this :

    $str = "{\"meetings\":[{
        \"uuid\":\"/random number==\",
        \"meeting_number\":7196503037,
        \"host_id\":\"random number\",
        \"topic\":\"My's Personal Meeting Room\",
        \"start_time\":\"2015-01-06T22:01:07Z\",
        \"timezone\":\"America/Denver\",
        \"duration\":56,
        \"total_size\":378080,
        \"recording_count\":1,
        \"recording_files\":[
            {
                \"id\":\"long number\",
                \"meeting_id\":\"/NS90bsSTLeGzo6cT0nwXw==\",
                \"recording_start\":\"2015-01-06T22:01:09Z\",
                \"recording_end\":\"2015-01-06T22:01:14Z\",
                \"file_size\":378080,
                \"play_url\":\"https://myurl\"
            }
        ]
    }]}";
    
    
    $json = json_decode($str);
    $res = $json->{'meetings'};
    
    foreach($res as $keys){
        echo $keys->{'start_time'}."<br>";
        foreach ($keys->{'recording_files'} as $data){
            echo $data->{'play_url'}."<br>";
            echo $data->{'recording_start'}."<br>";
        }
    }