Php 访问数组中的SimpleXmlElement

Php 访问数组中的SimpleXmlElement,php,arrays,simplexml,Php,Arrays,Simplexml,我已经看过类似的文章,比如,我不能让它工作,很可能我只是误解了 我有一个简单的脚本,它解析一些xml并打印出特定的字段——我遇到的问题是访问SimpleXMLElement对象的数据 XML(为清晰起见简化) 到目前为止,一切似乎都很好。我最终得到了一系列我可以确认的内容,这就是我得到的: Array ( [title] => SimpleXMLElement Object ( [0] => Title is in here ...

我已经看过类似的文章,比如,我不能让它工作,很可能我只是误解了

我有一个简单的脚本,它解析一些xml并打印出特定的字段——我遇到的问题是访问SimpleXMLElement对象的数据

XML(为清晰起见简化)

到目前为止,一切似乎都很好。我最终得到了一系列我可以确认的内容,这就是我得到的:

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 ($xml->channel->item as $item) {
    $articles = array();
    $articles['title'] = $item->title;
    $articles['description'] = $item->description;
}
如果您有foreach,为什么要在每个步骤上创建新的数组$articles=array()


如果您确实不想简单地传递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>');
}

您想将每个$item->title和$item->description填充到一个数组中有什么特殊原因吗?也就是说,为什么不能在需要的地方简单地使用simplexmlelement呢?可能有许多标题/描述集-我想将它们收集在一起,然后用另一个过程对它们进行迭代。。。如果这听起来像是一个错误的过程,尽管我希望得到其他建议:)即使这样,你也最多只能得到一篇文章,因为下一次迭代覆盖了旧的值。完全正确,到目前为止,我只使用一组值进行了测试-尽管我的代码很糟糕,但主要问题仍然存在:)解决了。不知道(字符串)$object->value。。。非常有用。
foreach ($articles as $article) {
        echo $article['title'][0];
    }
foreach ($articles as $article) {
        echo $article['title'];
    }
foreach ($xml->channel->item as $item) {
    $articles = array();
    $articles['title'] = $item->title;
    $articles['description'] = $item->description;
}
$articles = array();
foreach ($xml->channel->item as $item) {
        $articles['title'] = $item->title;
        $articles['description'] = $item->description;
}
<?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!