Take items from the JSON array

Asked

Viewed 709 times

2

Guys, I am working with youtube api and a query returns me the json below. How do I pick and print each item separately? For example I want to print each "videoId". How do I?

{
 "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"
    }
   }
  }
 ]
}

1 answer

3


To key that you’re looking for, doesn’t always stay in the same place, so with a recursive function, sweep all elements of the array in search of the keys with a certain name. The function was also used json_decode to turn data into text json for 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 example


Edit 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>';
    }
}

Example of code - after comment

  • Opa cara...vlw amigo. I understood what you said and it worked. You can help me like this: now that I realize that I only want the ids that have the "contentDetails": { "upload": {. I want these dis. Can you help me?

  • @Thiago was edited after his comment.

  • Perfect friend! Help me...only this error appears "PHP Notice: Undefined index: upload"...but it already helps me! I’ll mark it as a sure thing, but if there’s one for that it would be very helpful.

  • @Thiago I edited to not have the message. On mobile is complicated

  • 1

    Perfect. It worked!!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.