1

I'm extracting data from a JSON file and have no problem getting all the items except for one because it is an array within the JSON array.

Here is a sample of the JSON array:

{
 "items": [
  {
   "id": "12345",
   "snippet": {
    "publishedAt": "2015-07-25T02:43:39.000Z",
    "channelTitle": "BuzzFeedVideo",
    "tags": [
     "celebrity",
     "celebrities",
     "seven",
     "seven years",
     "years",
     "different",
     "now",
     "then",
     "ariana grande",
     "justin bieber",
     "calvin harris",
     "miley cyrus",
     "abigail breslin",
     "daniel radcliffe",
     "neville longbottom",
     "matthew lewis",
     "buzzfeed",
     "buzzfeedvideo",
     "buzzfeedyellow",
     "video"
    ],
    "defaultAudioLanguage": "en"
   },
   "statistics": {
    "viewCount": "700146",
    "likeCount": "16847",
    "dislikeCount": "596",
    "favoriteCount": "0",
    "commentCount": "1563"
   }
  }
 ]
}

I can get id, snippet:publishedAt, but when it comes to tags, only the last item in the tags array "video" is extracted with my foreach loop.

Here is my foreach loop:

$tags = $videosResponse['items'][0]['snippet']['tags'];

foreach($tags as $tag):
    $keywords = $tag;
    echo $keywords;
endforeach;

How do I get all the items within the JSON tags array starting from "celebrity" down to "video"?

UPDATE:

What I'm trying to do is update a php array file with the JSON data using file_put_contents.

Here is the structure I'm using.

    $tags = $videosResponse['items'][0]['snippet']['tags'];

    foreach($tags as $tag):
    $keywords = strtolower(replace_utf8_chars($tag));

    $new_array_line = " '" . $id . "' => array\n\t(\n\t'datetime' => '" . $publishedAt . "',\n\t'title' => '" . addslashes($title) . "',\n\t'description' => '" . addslashes($description) . "',\n\t'channel title' => '" . addslashes($channelTitle) . "',\n\t'tags' => '" . $keywords . "',\n\t'duration' => '" . $duration . "',\n\t'viewCount' => '" . $viewCount . "',\n\t'likeCount' => '" . $likeCount . "',\n\t'commentCount' => '" . $commentCount . "',\n\t'url' => '" . $url . "'\n\t),";
    endforeach;

and then the array pop and file put contents code is just below the $new_array_line variable.

    if(!array_key_exists($id, $videoids)) {
        $id_content = file($videoids_file); // Parse file into an array by newline
        $id_data = array_pop($id_content);

        if (trim($id_data) == ');?>') {
            $id_content[] = "\n" . '    // ' . $_SERVER['REMOTE_ADDR'] . ' | ' . date('M j, y h:i:s A') . "\n"; // Echo debug line
            $id_content[] = $new_array_line;
            $id_content[] = "\n".$id_data;
            file_put_contents($videoids_file, implode($id_content));
        }

        return array('title' => $title, 'description' => $description, 'channelTitle' => $channelTitle);

    }

If I end the foreach after this last part, then the first item in the tags array is output, but if I end the foreach before the if statement, then only the last item in the tags array is output.

Do I need some sort of time delay before the if statement is executed?

17
  • how is all your json? Commented Jul 25, 2015 at 22:49
  • it does not look like tags is in snippet, but a sibling -> ... "snippet": {"publishedAt": "2015-07-25T02:43:39.000Z", }, "tags": [..., so not sure why you have $videosResponse['items'][0]['snippet']['tags']; and not $videosResponse['items'][0]['tags']; Commented Jul 25, 2015 at 22:49
  • tags is a sibling of snippet. trust me. Commented Jul 25, 2015 at 23:12
  • 1
    i tested your json and it is built wrong. you can tested here Commented Jul 25, 2015 at 23:16
  • 1
    are you really doing echo in the foreach loop? not assigning it to some variable and overriding it each time until keeping the last one which is 'video' ? Commented Jul 25, 2015 at 23:49

2 Answers 2

2

$new_array_line = need to by replaced by $new_array_line .= otherwise the foreach loop will keep overriding it when evaluating the next tag value until keeping the last one which is related to the 'video' tag.

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

Comments

-1

never heared of endforeach... When the JSON is valid, you should be able to access the array with:

$videosResponse['items'][0]['tags'];

1 Comment

That's not what is causing it. Bracket or the other way, both ways produce same results.

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.