php对象转为数组(去除key中的类名)
/**
* 对象转array
* @param array $data
* @return array
*/
function object_to_array($data){
if ((! is_array($data)) and (! is_object($data))) return 'xxx'; //$data;
if(is_object($data)){
$needle = get_class($data);
}else{
$needle = '';
}
$result = array();
$data = (array) $data;
foreach ($data as $key => $value) {
if (is_array($value) || is_object($value) ){
$result[str_replace_once($needle,'',$key)] = object_to_array($value);
}else
$result[str_replace_once($needle,'',$key)] = $value;
}
return $result;
}
/**
* 替换一次
* @param string $needle
* @param string $replace
* @param string $haystack
* @return string
*/
function str_replace_once($needle, $replace, $haystack) {
$pos = @strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}