PHP foreach数组ID
我有一个foreach循环,它应该通过JSON循环,并使用youtubeapi返回JSON中列出的每个视频的相应ID。 这是我的密码:PHP foreach数组ID,php,arrays,json,foreach,youtube-api,Php,Arrays,Json,Foreach,Youtube Api,我有一个foreach循环,它应该通过JSON循环,并使用youtubeapi返回JSON中列出的每个视频的相应ID。 这是我的密码: class Videos { private $mVideoUrl; function setVideoTitle($videoUrl){ $this->mVideoUrl= $videoUrl; } function getVideoTitle(){ return $this->m
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和其他信息
我觉得这是一个简单的解决方案,但我无法用我的一生来解决它。
我将感谢任何帮助!
此外,如果我在这方面完全错了,建议也很感激
谢谢 在您的代码中,您的课堂视频包含两个函数
setVideoTitle(...),
getVideoTitle()
但在foreach中,您调用了$videos->getVideoUrl(),$videos->setVideoUrl(…)
这是什么?在您的代码中,您的课堂视频包含两个函数
setVideoTitle(...),
getVideoTitle()
但在foreach中,您调用了$videos->getVideoUrl(),$videos->setVideoUrl(…)
这是什么?我对你的代码做了一些修改。我修改了你的课程。我已将plurar视频重命名为视频(单数) 然后我添加了一个属性$id,因为属性的名称应该很简单,并且表示我们想要存储在其中的数据 然后我为$id属性添加了getter和setter 我不知道$url,所以我只编写了简单的JSON字符串。我试图模仿您在代码中使用的结构 然后我在newvideo()的末尾添加了()以调用适当的构造函数 我没有将元素推入数组,而是使用适当的$array[$index]=赋值 最后一件事,我已经从foreach循环中写出了数据。如果重定向到另一个文件,我将使用var_导出来获得适当的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);
我对你的代码做了一些修改。我修改了你的课程。我已将plurar视频重命名为视频(单数)
然后我添加了一个属性$id,因为属性的名称应该很简单,并且表示我们想要存储在其中的数据
然后我为$id属性添加了getter和setter
我不知道$url,所以我只编写了简单的JSON字符串。我试图模仿您在代码中使用的结构
然后我在newvideo()的末尾添加了()以调用适当的构造函数
我没有将元素推入数组,而是使用适当的$array[$index]=赋值
最后一件事,我已经从foreach循环中写出了数据。如果重定向到另一个文件,我将使用var_导出来获得适当的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);
echo$vidArray[0]代码>仅回显第一个元素。尝试打印($vidArray)代码>echo$vidArray[0]代码>仅回显第一个元素。尝试打印($vidArray)代码>