<转>php简单对象与数组的转换

最近用到一些简单的对象与数组的相互转换的问题,采用递归写了两个方法如下

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;
}


本文的原始链接为:http://www.cnblogs.com/jaiho/archive/2011/05/17/2049369.html转载请注明原始链接。

posted on 2015-10-20 18:53  hahahahahai12  阅读(136)  评论(0)    收藏  举报

导航