【问题标题】:Why is PHP Foreach Loop only extracting last item in the element of JSON Array?为什么 PHP Foreach 循环只提取 JSON 数组元素中的最后一项?
【发布时间】:2015-07-25 22:38:22
【问题描述】:

我正在从 JSON 文件中提取数据,并且除了一个之外的所有项目都没有问题,因为它是 JSON 数组中的一个数组。

这是 JSON 数组的示例:

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

我可以得到 id,sn-p:publishedAt,但是当涉及到标签时,只有标签数组 "video" 中的最后一项是用我的 foreach 循环提取的。

这是我的 foreach 循环:

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

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

如何获取 JSON 标记数组中从 "celebrity""video" 的所有项目?

更新:

我要做的是使用 file_put_contents 用 JSON 数据更新一个 php 数组文件。

这是我正在使用的结构。

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

然后数组 pop 和 file put 内容代码就在 $new_array_line 变量的下方。

    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);

    }

如果我在最后一部分之后结束 foreach,则输出 tags 数组中的第一项,但如果我在 if 语句之前结束 foreach,则只输出 tags 数组中的最后一项。

在执行 if 语句之前是否需要某种时间延迟?

【问题讨论】:

  • 你的json怎么样?
  • 它看起来不像 tagssnippet 中,而是一个兄弟 -> ... "snippet": {"publishedAt": "2015-07-25T02:43:39.000Z", }, "tags": [...,所以不知道为什么你有 $videosResponse['items'][0]['snippet']['tags']; 而不是 $videosResponse['items'][0]['tags'];
  • tags 是 sn-p 的兄弟。相信我。
  • 我测试了你的 json 并且构建错误。你可以测试here
  • 你真的在foreach循环中做echo吗?不要将它分配给某个变量并每次都覆盖它,直到保留最后一个“视频”?

标签: php arrays json foreach


【解决方案1】:

$new_array_line = 需要替换为 $new_array_line .= 否则 foreach 循环将在评估下一个标签值时继续覆盖它,直到保留与“video”标签相关的最后一个。

【讨论】:

    【解决方案2】:

    从未听说过 endforeach... 当 JSON 有效时,您应该能够通过以下方式访问数组:

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

    【讨论】:

    • 这不是造成它的原因。括号或其他方式,两种方式产生相同的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-21
    • 2020-07-31
    • 2012-02-10
    • 2019-10-22
    • 2013-07-15
    • 2010-10-14
    相关资源
    最近更新 更多