2010-08-19 74 views
0

我已經看過類似的文章,例如this one,我無法完成它的工作,很可能我只是誤會。訪問數組中的SimpleXMLElements

我有一個簡單的腳本,它解析了一些xml並打印出特定的字段 - 我遇到麻煩的是訪問SimpleXMLElement對象的數據。

XML(簡化爲清楚起見)

<channel> 
    <item> 
    <title><![CDATA[Title is in here ...]]></title> 
    <description>Our description is in here!</description> 
    </item> 
</channel> 

PHP

$url = "file.xml"; 
$xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA); 

foreach ($xml->channel->item as $item) { 
$articles = array(); 
$articles['title'] = $item->title; 
$articles['description'] = $item->description; 
} 

到現在爲止,一切似乎確定。我最終的內容的陣列,我可以用print_r確認,這是我得到的結果:

Array 
(
    [title] => SimpleXMLElement Object 
     (
      [0] => Title is in here ... 
     ) 

    [description] => SimpleXMLElement Object 
     (
      [0] => Our description is in here! 
     ) 
) 

關鍵的問題

我如何再進入[標題] [0]或[描述] [0]?

我已經嘗試了一些沒有成功的變體,很可能是某處的菜鳥錯誤!

foreach ($articles as $article) { 
     echo $article->title; 
    } 

foreach ($articles as $article) { 
     echo $article['title'][0]; 
    } 

foreach ($articles as $article) { 
     echo $article['title']; 
    } 
+0

有一個特殊的原因爲什麼你想要在數組中填充每個$ item-> title和$ item-> description?即爲什麼你不能簡單地在你需要的地方使用簡單元素? – VolkerK 2010-08-19 12:27:43

+0

可能會有大量的標題/描述 - 我想將它們聚集在一起,然後用另一個過程遍歷它們...如果聽起來像錯誤的過程,雖然我會喜歡其他建議:) – suitedupgeek 2010-08-19 12:35:33

回答

1

如果你真的不想簡單地傳遞的SimpleXMLElement但把值數組第一....

<?php 
// $xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA); 
$xlm = getData(); 

$articles = array(); 
foreach ($xml->channel->item as $item) { 
    // with (string)$item->title you get rid of the SimpleXMLElements and store plain strings 
    // but you could also keep the SimpleXMLElements here - the output is still the same. 
    $articles[] = array(
    'title'=>(string)$item->title, 
    'description'=>(string)$item->description 
); 
} 

// .... 
foreach($articles as $a) { 
    echo $a['title'], ' - ', $a['description'], "\n"; 
} 

function getData() { 
    return new SimpleXMLElement('<foo><channel> 
    <item> 
     <title><![CDATA[Title1 is in here ...]]></title> 
     <description>Our description1 is in here!</description> 
    </item> 
    <item> 
     <title><![CDATA[Title2 is in here ...]]></title> 
     <description>Our description2 is in here!</description> 
    </item> 
    </channel></foo>'); 
} 

打印

Title1 is in here ... - Our description1 is in here! 
Title2 is in here ... - Our description2 is in here! 
+0

釘住了它。不知道(字符串)$ object->值...非常有幫助。 – suitedupgeek 2010-08-19 12:43:48

0

我認爲當你將值分配給數組你有一個錯誤:

foreach ($xml->channel->item as $item) { 
    $articles = array(); 
    $articles['title'] = $item->title; 
    $articles['description'] = $item->description; 
} 

,如果你有的foreach爲什麼你在每一步創建新數組$ articles = array();

$articles = array(); 
foreach ($xml->channel->item as $item) { 
     $articles['title'] = $item->title; 
     $articles['description'] = $item->description; 
} 
+0

即使那樣你只能得到自下一次迭代以來最多一篇文章覆蓋舊值。 – VolkerK 2010-08-19 12:29:57

+0

完全正確,迄今爲止我只用一組數值進行了測試 - 主要問題仍然存在,儘管我的蹩腳代碼:) – suitedupgeek 2010-08-19 12:33:30