Picking items from the JSON array

2

I'm working with api from youtube and a query returns the json below. How do I get and print each item separately? For example I want to print each "videoId". How do I do?

{
 "kind": "youtube#activityListResponse",
 "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/oqpVLfMmbzY_6j3oThpHVqTWBFU\"",
 "nextPageToken": "CAUQAA",
 "pageInfo": {
  "totalResults": 19,
  "resultsPerPage": 5
 },
 "items": [
  {

   "kind": "youtube#activity",
   "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/dEwBoeecvF1DxUljodxRGtUsS_I\"",
   "id": "VTE0NzQ3NDkwMDExNDAxMjU5MzQzMzU1MDQ=",
   "contentDetails": {
    "upload": {
     "videoId": "H0fujYAi_qc"
    }
   }
  },
  {

   "kind": "youtube#activity",
   "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/yZ6GjvR50A58SJxa_k7ATKpuoyY\"",
   "id": "QlVMMTQ3NDc0OTAwMTE0MDEyNTkzNDMzNzc0NA==",
   "contentDetails": {
    "bulletin": {
     "resourceId": {
      "kind": "youtube#video",
      "videoId": "H0fujYAi_qc"
     }
    }
   }
  },  
  {

   "kind": "youtube#activity",
   "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/S4iTb5HgOBL_i1Wz57Yf-FT8Jn0\"",
   "id": "VTE0NzQ3MjgzOTYxNDAxMjU5MzQzMzY5NzY=",
   "contentDetails": {
    "upload": {
     "videoId": "DrAsh8EfH7I"
    }
   }
  }
 ]
}
    
asked by anonymous 25.09.2016 / 00:32

1 answer

3

The key you are looking for does not always stay in the same place, so with a Recursive function , scan all elements of array to search for keys with a given name . The json_decode function was also used to transform the data into text json to array .

<?php

$json = '{
 "kind": "youtube#activityListResponse",
 "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/oqpVLfMmbzY_6j3oThpHVqTWBFU\"",
 "nextPageToken": "CAUQAA",
 "pageInfo": {
    "totalResults": 19,
  "resultsPerPage": 5
 },
 "items": [
  {

      "kind": "youtube#activity",
   "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/dEwBoeecvF1DxUljodxRGtUsS_I\"",
   "id": "VTE0NzQ3NDkwMDExNDAxMjU5MzQzMzU1MDQ=",
   "contentDetails": {
      "upload": {
          "videoId": "H0fujYAi_qc"
    }
   }
  },
  {
  "kind": "youtube#activity",
  "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/yZ6GjvR50A58SJxa_k7ATKpuoyY\"",
  "id": "QlVMMTQ3NDc0OTAwMTE0MDEyNTkzNDMzNzc0NA==",
  "contentDetails": {
      "bulletin": {
          "resourceId": 
          {
              "kind": "youtube#video",
             "videoId": "H0fujYAi_qc"
          }
      }
   }
  },  
  {
   "kind": "youtube#activity",
   "etag": "\"I_8xdZu766_FSaexEaDXTIfEWc0/S4iTb5HgOBL_i1Wz57Yf-FT8Jn0\"",
   "id": "VTE0NzQ3MjgzOTYxNDAxMjU5MzQzMzY5NzY=",
   "contentDetails": {
      "upload": {
          "videoId": "DrAsh8EfH7I"
    }
   }
  }
 ]
}';
<?php    
$array = json_decode($json, true);

function find_by_name_key($name, array $array, array &$values)
{
    foreach ($array as $key => $value)
    {
        if ($key === $name)
        {
            $values[$name][] = $value;
        }
        if (is_array($value)) find_by_name_key($name, $value, $values);
    }
}

$values = array();
find_by_name_key('videoId', $array['items'], $values);
var_dump($values);

Code sample

Editing after comment

$array = json_decode($json, true);
foreach ($array['items'] as $key => $values)
{
    if (isset($values['contentDetails']['upload']) && is_array($values['contentDetails']['upload'])){
        echo $values['contentDetails']['upload']['videoId'];
        echo '<br>';
    }
}

Code sample - after comment

    
25.09.2016 / 00:58