2013-11-04 89 views
1

我有一個使用simplexml_load_file獲得的XML feed的php參數。 這裏是我的一些參數var_dump,所以你會看到結構。數組中的foreach對象在循環工作時不起作用

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" }

正如你可以看到有一個名爲「項」的數組。 我只是試圖做的是創建一個foreach循環,echo「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循環工作?

回答

-2

這應該工作

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

EDIT(其他方式):

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

作品!!!這是很奇怪......能否請您解釋一下爲什麼它的工作本辦法? – nimi

+1

你可以看到,$ video也是一個數組。但是你已經將它作爲一個變量來訪問,這就是爲什麼你沒有得到結果。 –

+1

你把一個半克隆(;)放在foreach中。使用'foreach($ videos-> channel-> item as $ video)'而​​不是'foreach($ videos-> channel-> item as $ video);' –