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

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

Here is my code:

$string = << 

 
  hello
  there<         


        
5条回答
  •  萌比男神i
    2021-01-04 20:39

    what you might want to do is using the Json encode/decode

    $jsonArray = Json_decode(Json_encode($xml), true);
    

    With the true argument you can call instead of using -> use [name]

    so an example would be:

    $xml = file("someXmlFile.xml");
    $jsonArray = Json_decode(Json_encode($xml), true);
    foreach(jsonArray['title'] as $title){
       Do something with $titles
    }
    

    if you have more than 1 element it will typical put in an @attributes if the elements has attributes. This can be countered by using: $title = $title['@attributes']

    Hope it could help.

提交回复
热议问题