PHP将带有SimpleXMLElement对象的数组转换为XML
我有一个数组,里面有一些SimpleXMLElement对象,现在我需要得到一个用于Ajax交互的格式良好的XML,我该怎么做 这是阵列:PHP将带有SimpleXMLElement对象的数组转换为XML,php,arrays,xml,simplexml,Php,Arrays,Xml,Simplexml,我有一个数组,里面有一些SimpleXMLElement对象,现在我需要得到一个用于Ajax交互的格式良好的XML,我该怎么做 这是阵列: Array ( [0] => SimpleXMLElement Object ( [count] => 2 [id] => 20 [user_id] => 2 [title] => Polo RL ) [1] => Sim
Array (
[0] => SimpleXMLElement Object (
[count] => 2
[id] => 20
[user_id] => 2
[title] => Polo RL )
[1] => SimpleXMLElement Object (
[count] => 3
[id] => 19
[user_id] => 4
[title] => tshirt fitch )
[2] => SimpleXMLElement Object (
[count] => 2
[id] => 18
[user_id] => 2
[title] => Polo La Martina )
)
我将得到以下XML结果:
<root>
<record>
<count>2</count>
<id>20</id>
<user_id>2</user_id>
<title>Polo RL</title>
</record>
<record>
<count>3</count>
<id>19</id>
<user_id>4</user_id>
<title>tshirt fitch</title>
</record>
<record>
<count>2</count>
<id>18</id>
<user_id>2</user_id>
<title>Polo La Martina</title>
</record>
</root>
2.
20
2.
马球
3.
19
4.
T恤惠誉
2.
18
2.
马丁纳马球
我将使用SimpleXMLElement的asXML方法输出每个对象的XML。因此:
$xml = <<<XML
<record>
<count>2</count>
<id>20</id>
<user_id>2</user_id>
<title>Polo RL</title>
<record>
XML;
$xml = new SimpleXMLElement($xml);
echo $xml->asXML();
$xml=asXML());
}
$fullXml.='';
echo$fullXml;
您尝试过的是创建数组的代码吗?这里有很多方法是“从数组转换为xml”,但是没有人在数组中有SimpleXMLElement对象,很抱歉我的简短回答,但我没有太多时间!它最初是一个与我编写的XML类似的XML,但它没有按降序排列id,然后我使用迭代器\u to\u array、array\u multisort和usort以正确的方式排列它,现在我需要返回到XML。是否要将它们合并到一个对象中?或者想要格式化字符串?谢谢!这正是我需要的!
<record>
<count>2</count>
<id>20</id>
<user_id>2</user_id>
<title>Polo RL</title>
<record>
$fullXml = '<root>';
foreach($arrXml as $xmlElement){
$fullXml .= str_replace('<?xml version="1.0"?>', '',$xmlElement->asXML());
}
$fullXml .= '</root>';
echo $fullXml ;