PHP XML转数组,对象转数组
2021-05-13 06:30
标签:simple code UNC else imp ml2 dcl dump string PHP XML转数组,对象转数组 标签:simple code UNC else imp ml2 dcl dump string 原文地址:https://www.cnblogs.com/XiaoMingBlingBling/p/13130958.html
$xml =
XML
测试XML测试XML>
测试XML测试XML>
测试XML测试XML>
测试XML测试XML>
测试XML>
测试XML>
XML;
$obj = new stdClass();
$obj -> 测试对象1 = ‘测试对象‘;
$obj -> 测试对象2 = new stdClass();
$obj -> 测试对象2 -> 测试对象1 = ‘测试对象‘;
$obj -> 测试对象2 -> 测试对象2 = new stdClass();
$obj -> 测试对象2 -> 测试对象2 -> 测试对象1 = ‘测试对象‘;
$obj -> 测试对象2 -> 测试对象2 -> 测试对象2 = ‘测试对象‘;
function xmsb_xml2arr($xml)
{
$obj = simplexml_load_string($xml);
$arr[$obj -> getName()] = xmsb_obj2arr($obj);
return $arr;
}
function xmsb_obj2arr($obj)
{
$arr = (array) $obj;
foreach($arr as &$child)
{
if(is_array($child))
{
foreach($child as &$childs)
{
if(is_object($childs))
{
$childs = xmsb_obj2arr($childs);
}
}
}
else if(is_object($child))
{
$child = xmsb_obj2arr($child);
}
}
return $arr;
}
// xml转数组
var_dump(xmsb_xml2arr($xml));
// 对象转数组
var_dump(xmsb_obj2arr($obj));