php 警告:array_slice() 期望参数 1 是数组,在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5372533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning: array_slice() expects parameter 1 to be array, null given in
提问by kanso
I'm using the following code in my WordPress site to display the latest Youtube video from my account using rss:
我在我的 WordPress 站点中使用以下代码通过 rss 显示我帐户中的最新 Youtube 视频:
<?php
include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
$feed = fetch_rss('http://gdata.youtube.com/feeds/api/users/UrbanGAME/uploads'); // specify feed url
$items = array_slice($feed->items, 0, 1); // specify first and last item
?>
<?php if (!empty($items)) : ?>
<?php foreach ($items as $item) : ?>
<object width="96%"><param name="movie" value="<?php echo str_replace("watch?v=","v/", $item['link']); ?>&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="<?php echo str_replace("watch?v=","v/", $item['link']); ?>&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="96%"></embed></object>
<?php endforeach; ?>
<?php endif; ?>
It works. However, some of the times I get this error when it does not work:
有用。但是,有时我会在它不起作用时收到此错误:
Warning: array_slice() expects parameter 1 to be array, null given in /wp-content/themes/theme1/header.php on line 173.
Also I'm using the above code twice, for 2 spots on the site for 2 different accounts. One is in the my header.php file and the other in my footer.php file. When 1 works, the other does not, and of course I get an error/warning for the other one as well, just referring to that file and a different line. I have tried messing with the code and still nothing. Could the problem be with something in my code?
此外,我两次使用上述代码,分别用于 2 个不同帐户的站点上的 2 个位置。一个在我的 header.php 文件中,另一个在我的 footer.php 文件中。当 1 工作时,另一个不工作,当然我也会收到另一个错误/警告,只是指的是该文件和不同的行。我试过弄乱代码,但仍然没有。问题可能出在我的代码中吗?
Additional info:
附加信息:
php: 5.3.2
php: 5.3.2
host: crazy domains (http://www.crazydomains.com.au/)
主机:疯狂域(http://www.crazydomains.com.au/)
apache: 2.2.15
阿帕奇:2.2.15
回答by Mike Lewis
The reason it may not work is because $feed->items
may not be an array(based on the fetch_rss
call).
它可能不起作用的原因是因为$feed->items
可能不是数组(基于fetch_rss
调用)。
To check if something is an array, you can do:
要检查某个东西是否是一个数组,您可以执行以下操作:
is_array($feed->items);
// returns true or false
is_array($feed->items);
// 返回真或假
If it is an array, you can then call array_slice()
如果它是一个数组,则可以调用 array_slice()
The better solution however, is to check the $feed
variable to make sure it returned what you are looking for before processing.
然而,更好的解决方案是检查$feed
变量以确保它在处理之前返回您正在寻找的内容。
回答by jon_darkstar
Looks like $feed->items
may or may not be an array. That all depends on how $feed
gets assigned, which depends on whats happening inside fetch_rss
看起来$feed->items
可能是也可能不是数组。这一切都取决于如何$feed
分配,这取决于内部发生的事情fetch_rss
Although tis somewhat oblique to your problem, it appears you have some superfluous code. You do the slice to get the first element the array alone, and then you iterate on a list with just that one element? Instead you can simply perform whatever you like on that first element.
尽管这与您的问题有些不同,但您似乎有一些多余的代码。您执行切片以单独获取数组的第一个元素,然后仅使用该元素对列表进行迭代?相反,您可以简单地在第一个元素上执行任何您喜欢的操作。
<?php
include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
$feed = fetch_rss('http://gdata.youtube.com/feeds/api/users/UrbanGAME/uploads'); // specify feed url
$firstItem = exists($feed->items[0]) ? $feed->items[0] : null;
?>
<?php if ($firstItem) : ?>
<object width="96%"><param name="movie" value="<?php echo str_replace("watch?v=","v/", $item['link']); ?>&hl=en_GB&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="<?php echo str_replace("watch?v=","v/", $firstItem['link']); ?>&hl=en_GB&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="96%"></embed></object>
<?php endif; ?>
回答by Dexter
I've had this same problem with an RSS feed related to a theme I'm using. On the admin side, the code is supposed to display more themes by the same artist, however I'm getting the same array_slice error, because $feed->items is null.
我在与我正在使用的主题相关的 RSS 提要中遇到了同样的问题。在管理方面,代码应该显示同一艺术家的更多主题,但是我收到了相同的 array_slice 错误,因为 $feed->items 为空。
The link to the RSS feed is working properly, and there is information at the address. So, I started looking around, and I found that the problem is with the RSS, not the code (well, the original problem. It's still a problem that that error is displayed). I pointed the fetch_rss()
command at a different feed, and it works fine.
RSS 提要的链接工作正常,地址中有信息。所以,我开始环顾四周,我发现问题出在RSS上,而不是代码(嗯,原来的问题。显示那个错误仍然是一个问题)。我将fetch_rss()
命令指向不同的提要,它工作正常。
Until the artist adjusts his feed to work with the command, there's nothing I can personally do. But if you're having trouble getting this command to work with your ownfeeds, try making sure the feeds are in the required format for this Wordpress command.
在艺术家调整他的提要以使用命令之前,我个人无能为力。但是,如果您在使用此命令处理您自己的提要时遇到问题,请尝试确保提要采用此 Wordpress 命令所需的格式。
Sorry to post on an old question, but there are no direct answers elsewhere on the web.
很抱歉发布一个旧问题,但网络上的其他地方没有直接答案。