/** * XML转化为数组 * * @author 何洋 2016-3-17 14:37:20 */ function xml_to_array($xml){ $reg = "/<(\w+)[^>]*?>([\x00-\xFF]*?)<\/\1>/"; if(preg_match_all($reg, $xml, $matches)){ $count = count($matches[0]); $arr = array(); for($i = 0; $i < $count; $i++){ $key= $matches[1][$i]; $val = xml_to_array( $matches[2][$i] ); // 递归 if(array_key_exists($key, $arr)){ if(is_array($arr[$key])){ if(!array_key_exists(0,$arr[$key])){ $arr[$key] = array($arr[$key]); } }else{ $arr[$key] = array($arr[$key]); } $arr[$key][] = $val; }else{ $arr[$key] = $val; } } return $arr; }else{ return $xml; } }
/** * 对象转数组 * * @author 何洋 2016-3-9 10:27:14 */ function object_array($obj){ $_arr = is_object($obj) ? get_object_vars($obj) :$obj; foreach ($_arr as $key=>$val){ $val = (is_array($val) || is_object($val)) ? object_to_array($val):$val; $arr[$key] = $val; } return $arr; }