警告:array_slice() 期望参数 1 为数组,在中给出 null

发布于 2024-10-24 19:02:36 字数 1468 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍景流年 2024-10-31 19:02:36

它可能不起作用的原因是 $feed->items 可能不是数组(基于 fetch_rss 调用)。

要检查某物是否是数组,您可以执行以下操作:

is_array($feed->items); // returns true or false

如果它是数组,您可以调用 array_slice( )

然而,更好的解决方案是检查 $feed 变量,以确保它在处理之前返回您要查找的内容。

The reason it may not work is because $feed->items may not be an array(based on the fetch_rss call).

To check if something is an array, you can do:

is_array($feed->items); // returns true or false

If it is an array, you can then call array_slice()

The better solution however, is to check the $feed variable to make sure it returned what you are looking for before processing.

旧伤还要旧人安 2024-10-31 19:02:36

看起来 $feed->items 可能是也可能不是数组。这一切都取决于 $feed 的分配方式,这取决于 fetch_rss 内部发生的情况,

尽管这对您的问题有些倾斜,但似乎您有一些多余的代码。您执行切片以单独获取数组的第一个元素,然后仅使用该一个元素迭代列表?相反,您可以简单地对第一个元素执行您喜欢的任何操作。

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

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

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; ?>
旧人哭 2024-10-31 19:02:36

我在使用与我正在使用的主题相关的 RSS 提要时也遇到过同样的问题。在管理方面,代码应该显示同一艺术家的更多主题,但是我收到相同的 array_slice 错误,因为 $feed->items 为空。

RSS 源的链接工作正常,并且该地址有信息。于是,我开始环顾四周,发现问题出在RSS上,而不是代码上(好吧,原来的问题。显示该错误仍然是一个问题)。我将 fetch_rss() 命令指向不同的提要,它工作得很好。

在艺术家调整他的提要以适应该命令之前,我个人无能为力。但是,如果您无法将此命令与您自己的提要一起使用,请尝试确保提要采用此 Wordpress 命令所需的格式。

很抱歉在一个老问题上发帖,但网络上的其他地方没有直接的答案。

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.

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.

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 own feeds, try making sure the feeds are in the required format for this Wordpress command.

Sorry to post on an old question, but there are no direct answers elsewhere on the web.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文