2010-08-19 52 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