【问题标题】:PHP foreach array IDsPHP foreach 数组 ID
【发布时间】:2016-09-08 19:29:12
【问题描述】:

我有一个 foreach 循环,它应该遍历 JSON,并使用 Youtube api 返回 JSON 中列出的每个视频的适当 ID。 这是我的代码:

class Videos {
    private $mVideoUrl;

    function setVideoTitle($videoUrl){
        $this->mVideoUrl= $videoUrl;
    }

    function getVideoTitle(){
        return $this->mVideoUrl;
    }
} 

$jsonFile = file_get_contents($url);
$jfo = json_decode($jsonFile);
$items = $jfo->items;
$vidArray = array();

foreach ($items as $item){
    if(!empty($item->id->videoId)){
        $Videos = new Videos;
        $Videos->setVideoUrl($item->id->videoId);
        $id = $Videos->getVideoUrl();
        array_push($vidArray, $id);
    }
    echo $vidArray[0];
}

问题是,数组推送工作正常,但是当我回显它时,它只是为每个循环迭代添加列表中的第一个 ID。当我回显 $id 变量时,它会很好地打印所有 ID。

最终我希望能够为每个视频创建一个对象,存储它的 ID 和其他信息。

我觉得这是一个简单的解决方法,但我一生都无法弄清楚。 我将不胜感激任何帮助! 此外,如果我对这一切都错了,也很感谢您的建议!

谢谢!

【问题讨论】:

  • echo $vidArray[0]; 只回显第一个元素。试试print_r($vidArray);

标签: php arrays json foreach youtube-api


【解决方案1】:

我对您的代码进行了一些尝试。我已经修改了你的课程。我已将复数视频重命名为视频(单数)。

然后我添加了一个属性 $id,因为属性的名称应该简单并且代表我们想要存储在其中的数据。

然后我为 $id 属性添加了 getter 和 setter。

我不知道 $url,所以我只写了简单的 JSON 字符串。我试图模仿您在代码中使用的结构。

然后我将 () 添加到 new Video() 的末尾以调用适当的构造函数。

我没有将元素推入数组,而是使用了正确的 $array[$index] = assignment。

最后,我已将数据从 foreach 循环中移出。如果重定向到另一个文件,我正在使用 var_export 获取正确的 php 代码。

<?php

class Video
{
    private $mVideoUrl;
    private $id; // added id attribute

    /**
     * @return mixed
     */
    public function getId() // added getter
    {
        return $this->id;
    }

    /**
     * @param mixed $id
     */
    public function setId($id) // added setter
    {
        $this->id = $id;
    }


    function setVideoTitle($videoUrl)
    {
        $this->mVideoUrl = $videoUrl;
    }

    function getVideoTitle()
    {
        return $this->mVideoUrl;
    }
}

// ignored for now
// $jsonFile = file_get_contents($url);
$jsonFile = '{"items": [
        { "id": { "videoId": 1, "url": "http://www.youtube.com/1" } },
        { "id": { "videoId": 2, "url": "http://www.youtube.com/2" } },
        { "id": { "videoId": 3, "url": "http://www.youtube.com/3" } },
        { "id": { "videoId": 4, "url": "http://www.youtube.com/4" } },
        { "id": { "videoId": 5, "url": "http://www.youtube.com/5" } }
    ]
}';

$jfo = json_decode($jsonFile);

$items = $jfo->items;
$vidArray = array();

foreach ($items as $item)
{
    if (!empty($item->id->videoId))
    {
        $Video = new Video(); // added brackets

        $Video->setId($item->id->videoId); // changed to setId
        $Video->setVideoTitle($item->id->url);
        $id = $Video->getId();
        $vidArray[$id] = $Video;
    }

}

// write out all data
var_export($vidArray);

【讨论】:

    【解决方案2】:

    在您的代码中,您的视频类包含两个函数

    setVideoTitle(...),
    getVideoTitle()
    

    但是在你的 foreach 中你调用了$videos-&gt;getVideoUrl() , $videos-&gt;setVideoUrl(...)

    这是什么???

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      • 2010-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 2011-06-09
      相关资源
      最近更新 更多