PHP XML转数组,对象转数组
$xml = <<<XML <测试XML> <测试XML>测试XML</测试XML> <测试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));
欢迎转载,转载时请注明来源。