$xml_obj = simplexml_load_file('xxx.xml');
public function simplexml_obj2array($xml_obj)
{
if ($obj instanceof SimpleXMLElement) {
$obj = (array)$obj;
}
if (is_array($obj)) {
$result = $keys = array();
foreach ( $obj as $key=>$value) {
isset($keys[$key]) ? ($keys[$key] += 1) : ($keys[$key] =
1);
if ( $keys[$key] == 1 ) {
$result[$key] = self::simplexml_obj2array($value);
} elseif ( $keys[$key] == 2 ) {
$result[$key] = array($result[$key],
self::simplexml_obj2array($value));
} else if( $keys[$key] > 2 ) {
$result[$key][] = self::simplexml_obj2array($value);
}
}
return $result;
} else {
return $obj;
}
}
XML数据解析与递归转换至数组的高效算法
319

被折叠的 条评论
为什么被折叠?



