2013-12-08 23 views
0

得到非對象的屬性中的錯誤,我必須明明在這裏做得不對:PHP的SimpleXML在業績預告:試圖從捲曲

我用捲髮來檢索超出保存爲$登錄數據結果。 XML看起來像:

<data> 
    <option>...</option> 
    <option>...</option> 
    <option>...</option> 
    <items> 
    <item title="label"> 
     <detail_1="abc"> 
     <detail_2="def"> 
    </item> 
    <item title="label2"> 
     <detail_1="def"> 
     <detail_2="ghi"> 
    </item> 
    </items> 
</data> 

我只需要來自每個'item'組的所有數據。

我的代碼:

$xml = simplexml_load_string($result); 

foreach($xml->data->items->item as $item) 
    { 

    echo $item["label"].'<br>'; 
    } 

結果是

Notice: Trying to get property of non-object in .... 

我以前沒有使用過該功能。請指教。

+0

我認爲你試圖去一個的屬性元素太深,不適合循環元素。嘗試$ xml-> data-> items作爲$ item。 – Tarnation

+0

謝謝,但似乎也沒有工作。我得到警告:爲foreach()提供的無效參數在... – user2029890

回答

0

取而代之的是echo $item["label"].'<br>';試試這個

echo $item['title'].'<br>'; 
0

你不喜歡這一點,因爲你想打印一個是項目的標籤

foreach($xml->data->items->item->attributes() as $key=>$val) 
    { 

    echo $val.'<br>'; 
    }