PHP xml转换成数组 20
XML中的内容为:<?xmlversion="1.0"encoding="utf-8"?><xml><list>1</list><list>2</list><list>3...
XML中的内容为:
<?xml version="1.0" encoding="utf-8"?>
<xml>
<list>1</list>
<list>2</list>
<list>3</list>
</xml>
PHP代码:
<?php
$s=join("",file('test.xml'));
$result = xml_to_array($s);
function xml_to_array($xml)
{
$array = (array)(simplexml_load_string($xml));
foreach ($array as $key=>$item){
$array[$key] = struct_to_array((array)$item);
}
return $array;
}
function struct_to_array($item) {
if(!is_string($item)) {
$item = (array)$item;
foreach ($item as $key=>$val){
$item[$key] = struct_to_array($val);
}
}
return $item;
}
print_r($result);
?>
打印出的内容为:Array ( [list] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
如何才能打印出:Array ( [0] => 1 [1] => 2 [2] => 3 ),这种形式的,我是想要这样的一个数组:array('1','2','3')
谢谢! 展开
<?xml version="1.0" encoding="utf-8"?>
<xml>
<list>1</list>
<list>2</list>
<list>3</list>
</xml>
PHP代码:
<?php
$s=join("",file('test.xml'));
$result = xml_to_array($s);
function xml_to_array($xml)
{
$array = (array)(simplexml_load_string($xml));
foreach ($array as $key=>$item){
$array[$key] = struct_to_array((array)$item);
}
return $array;
}
function struct_to_array($item) {
if(!is_string($item)) {
$item = (array)$item;
foreach ($item as $key=>$val){
$item[$key] = struct_to_array($val);
}
}
return $item;
}
print_r($result);
?>
打印出的内容为:Array ( [list] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
如何才能打印出:Array ( [0] => 1 [1] => 2 [2] => 3 ),这种形式的,我是想要这样的一个数组:array('1','2','3')
谢谢! 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询