欢迎各位兄弟 发布技术文章
这里的技术是共享的
自己亲自写的对象转数组
function _object_to_array($obj)
{
$arr = (array)$obj;
foreach ($arr as $k => $v) {
if (gettype($v) == 'resource') {
return;
}
if (gettype($v) == 'object' || gettype($v) == 'array') {
$arr[$k] = (array)_object_to_array($v);
}
}
return $arr;
}
最近用到一些简单的对象与数组的相互转换的问题,采用递归写了两个方法如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | function arrayToObject( $e ){ if ( gettype ( $e )!= 'array' ) return ; foreach ( $e as $k => $v ){ if ( gettype ( $v )== 'array' || getType ( $v )== 'object' ) $e [ $k ]=(object)arrayToObject( $v ); } return (object) $e ; } function objectToArray( $e ){ $e =( array ) $e ; foreach ( $e as $k => $v ){ if ( gettype ( $v )== 'resource' ) return ; if ( gettype ( $v )== 'object' || gettype ( $v )== 'array' ) $e [ $k ]=( array )objectToArray( $v ); } return $e ; } |