本文介绍了为什么SimpleXML的改变我的阵列到阵列的第一个元素,当我使用它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code:

$string = <<<XML
<?xml version='1.0'?> 
<test>
 <testing>
  <lol>hello</lol>
  <lol>there</lol>
 </testing>
</test>
XML;
$xml = simplexml_load_string($string);
echo "All of the XML:\n";
print_r $xml;
echo "\n\nJust the 'lol' array:";
print_r $xml->testing->lol;

输出:

All of the XML:

SimpleXMLElement Object
(
    [testing] => SimpleXMLElement Object
        (
            [lol] => Array
                (
                    [0] => hello
                    [1] => there
                )

        )

)




Just the 'lol' array:

SimpleXMLElement Object
(
    [0] => hello
)

为什么就只输出[0],而不是整个数组?我不明白这一点。

Why does it output only the [0] instead of the whole array? I dont get it.

推荐答案

这是因为你有两个笑的元素。为了访问你需要做的这第二个:

It's because you have two lol elements. In order to access the second you need to do this:

$xml->testing->lol[1];

这会给你那里

$xml->testing->lol[0];

会给你你好

孩子()为SimpleXMLElement的方法会给你含有例如元素的所有子对象:

The children() method of the SimpleXMLElement will give you an object containing all the children of an element for example:

$xml->testing->children();

会给你包含测试的SimpleXMLElement的所有子对象。

will give you an object containing all the children of the "testing" SimpleXMLElement.

如果您需要迭代,你可以使用下面的code:

If you need to iterate, you can use the following code:

foreach($xml->testing->children() as $ele)
{
    var_dump($ele);
}

有这里是有关的SimpleXMLElement的更多信息:

There is more information about SimpleXMLElement here:

http://www.php.net/manual/en/class.simplexmlelement.php

这篇关于为什么SimpleXML的改变我的阵列到阵列的第一个元素,当我使用它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 04:54