【问题标题】:foreach object in array won't work while for loop working数组中的foreach对象在for循环工作时不起作用
【发布时间】:2013-11-14 22:29:22
【问题描述】:

我有一个使用 simplexml_load_file 获得的 XML 提要的 php 参数。 这是我的一些参数的 var_dump,因此您将看到结构。

object(SimpleXMLElement)#1 (2) { ["@attributes"]=> array(1) { ["version"]=> string(3) "2.0" } ["channel"]=> object(SimpleXMLElement)#2 (5) { ["title"]=> string(20) "BWM: CarolinaCooking" ["link"]=> string(69) "http://mybluewavemedia.com/portal/v2/feeds/ovg/45_CarolinaCooking.xml" ["description"]=> string(43) "Blue Wave Media Video MRSS Feed for OVGuide" ["lastBuildDate"]=> string(31) "Mon, 04 Nov 2013 09:10:44 +0000" ["item"]=> array(29) { [0]=> object(SimpleXMLElement)#3 (4) { ["title"]=> string(32) "Vanilla Cream with Fresh Berries" ["description"]=> string(135) "Host Thom Zelenka and Chef Catherine Rabb of Fenwick's on Providence restaurant teach you how to make Vanilla Cream with Fresh Berries." ["guid"]=> string(22) "BWM-CLIENT45-ITEM34340" ["pubDate"]=> string(31) "Thu, 07 Feb 2013 05:43:12 +0000" } [1]=> object(SimpleXMLElement)#4 (4) { ["title"]=> string(33) "Meatloaf Stuffed with Blue Cheese" ["description"]=> string(153) "Host Thom Zelenka and Chef Catherine Rabb of Frenwick's on Providence restaruant teach you how to make Meatloaf Stuffed with Blue Cheese, Tomato & Bacon." ["guid"]=> string(22) "BWM-CLIENT45-ITEM34338" ["pubDate"]=> string(31) "Thu, 07 Feb 2013 05:43:12 +0000" } [2]=> object(SimpleXMLElement)#5 (4) { ["title"]=> string(23) "Creole Barbecued Shrimp" ["description"]=> string(127) "Host Thom Zelenka and Chef Catherine Rabb of Fenwick's on Provoidence restaurant teach you how to make Creole Barbecued shrimp." ["guid"]=> string(22) "BWM-CLIENT45-ITEM34336" ["pubDate"]=> string(31) "Thu, 07 Feb 2013 05:43:12 +0000" } [3]=> object(SimpleXMLElement)#6 (4) { ["title"]=> string(23) "Caribbean Banana Foster" ["description"]=> string(156) "Host Thom Zelenka and Chef Mathew Beard of the Speedway Club restaurant teach you how to make a Caribbean Bananas Foster with Toasted Coconut-Rum Ice Cream." ["guid"]=> string(22) "BWM-CLIENT45-ITEM34334" ["pubDate"]=> string(31) "Thu, 07 Feb 2013 05:43:12 +0000" } [4]=> object(SimpleXMLElement)#7 (4) { ["title"]=> string(19) "Seared Veal Cutlets" ["description"]=> string(155) "Host Thom Zelenka and Chef Mathew Beard of the Speedway Culb restaurant teach you how to make Seared Veal Cutlets with Pan Roasted Lobster and Potato Hash." ["guid"]=> string(22) "BWM-CLIENT45-ITEM34332" ["pubDate"]=> string(31) "Thu, 07 Feb 2013 05:43:12 +0000" }

如您所见,有一个名为“item”的数组。 我只是尝试做的是创建一个 foreach 循环,以回显“item”数组中每个对象的标题。 我这样做如下:

foreach($videos->channel->item as $video);
        {
        var_dump($video);
        echo "<BR><BR>";
        echo "TITLE: " . $video->title . "<BR>";
        }

这根本行不通。它只打印数组中的最后一个标题。

奇怪的是,当我以这种方式创建循环时,它确实起作用了:

$i=0;
    while($i<30) {
    echo $i . $videos->channel->item[$i]->title . "<BR>";
    $i++;
    }

谁能试着解释一下为什么它不能通过 foreach 循环工作?

【问题讨论】:

    标签: php foreach simplexml


    【解决方案1】:

    这应该可以工作

    $i=0;
    foreach($videos->channel->item as $video) {
    echo "TITLE: " . $video[$i]->title;
    $i++;
    }
    

    编辑(其他方式):

    foreach($videos->channel->item as $video) {
        foreach($video as $val)
         {
            echo "TITLE: " . $val->title;
         }
        }
    

    【讨论】:

    • 工作!!!这很奇怪......你能解释一下为什么它会这样工作吗?
    • 你可以看到,$video 也是一个数组。但是您已将其作为变量访问,这就是您没有得到结果的原因。
    • 你在 foreach 中放了一个半克隆(;)。使用foreach($videos-&gt;channel-&gt;item as $video) 而不是foreach($videos-&gt;channel-&gt;item as $video);
    • @Md.SahadatHossain,不,这不是问题。
    • 分号是主要问题,应该作为答案添加:它导致 foreach 循环包含零个语句,因此它一直循环并将 $video 设置为最后一个执行回显时循环的元素。这里的替代方案具有误导性。
    猜你喜欢
    • 2013-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 2021-04-26
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多