我正在尝试将以下xml的XML转换为PHP数组
$xml = <rss
xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<item>
<title>Product1</title>
<brand>Shoes</brand>
<g:price>89.95</g:price>
<g:sale_price>89.95</g:sale_price>
</item>
<item>
<title>Product2</title>
<brand>Shoes</brand>
<g:price>89.95</g:price>
<g:sale_price>89.95</g:sale_price>
</item>
</channel>
我要使用的代码是
$obj = simplexml_load_string($xml); // Parse XML
$array = json_decode(json_encode($obj), true); // Convert to array
var_dump($array);
以上代码仅针对不包含名称空间的xml标记转换为数组。
array(1) {
["item"]=>
array(2) {
[0]=>
array(2) {
["title"]=>
string(49) "Product1"
["brand"]=>
string(143) "shoes"
}
[1]=>
array(2) {
["title"]=>
string(28) "Product2"
["brand"]=>
string(122) "shoes"
}
}
}
任何人都可以帮助我如何从xml中删除名称空间并将其转换为类似于下面的输出的数组
首选输出是:
array(1) {
["item"]=>
array(2) {
[0]=>
array(2) {
["title"]=>
string(49) "Product1"
["brand"]=>
string(143) "shoes"
["price"]=>
int(143) "89.95"
["sale_price"]=>
int(143) "89.95"
}
[1]=>
array(2) {
["title"]=>
string(28) "Product2"
["brand"]=>
string(122) "shoes"
["price"]=>
int(143) "89.95"
["sale_price"]=>
int(143) "89.95"
}
}
}