Why does SimpleXML change my array to the array's first element when I use it?

前端 未结 5 1192
予麋鹿
予麋鹿 2021-01-04 20:18

Here is my code:

$string = << 

 
  hello
  there<         


        
5条回答
  •  忘掉有多难
    2021-01-04 20:45

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

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

    this will give you "there"

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

    Will give you "hello"

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

    $xml->testing->children();
    

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

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

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

    There is more information about SimpleXMLElement here:

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

提交回复
热议问题