Mike
Mike

Reputation: 607

Why is PHP Foreach Loop only extracting last item in the element of JSON Array?

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?

Upvotes: 1

Views: 1222

Answers (2)

Salem Ouerdani
Salem Ouerdani

Reputation: 7886

$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.

Upvotes: 2

Jeffrey
Jeffrey

Reputation: 1804

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

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

Upvotes: -1

Related Questions