gpt4 book ai didi

php - 使用 simplexml_load_string 时只返回数组的第一个元素

转载 作者:数据小太阳 更新时间:2023-10-29 02:45:09 25 4
gpt4 key购买 nike

我阅读了以下 xml 文件:

<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
<books uiTotalCount="45" uiTotalPages="2" >
<book title="Book1">
<registrationNumber formatCode="raw">BO1</registrationNumber>
</book>
<book title="Book2">
<registrationNumber formatCode="raw">BO2</registrationNumber>
</book>
</books>

使用以下代码时:

$xmlResponse = file_get_contents('\xml\books.xml', true);
xml = simplexml_load_string($xmlResponse);
var_dump($xml->book);

最后一行只给出第一个元素:

object(SimpleXMLElement)[3]
public '@attributes' =>
array (size=1)
'title' => string 'Book1' (length=5)
public 'registrationNumber' => string 'BO1' (length=3)`

当我查看 var_dump($xml) 时,我可以看到两本书:

object(SimpleXMLElement)[2]
public '@attributes' =>
array (size=2)
'uiTotalCount' => string '45' (length=2)
'uiTotalPages' => string '2' (length=1)
public 'book' =>
array (size=2)
0 =>
object(SimpleXMLElement)[3]
public '@attributes' =>
array (size=1)
...
public 'registrationNumber' => string 'BO1' (length=3)
1 =>
object(SimpleXMLElement)[4]
public '@attributes' =>
array (size=1)
...
public 'registrationNumber' => string 'BO2' (length=3)

有人知道我做错了什么吗?我想获得所有书籍的完整数组,以便我可以对其进行 jsonify。

最佳答案

通过完整的 xml 编写一个循环并将 XML 书籍对象存储在一个数组中是否是一种解决方案?

关于php - 使用 simplexml_load_string 时只返回数组的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24098871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com