当前位置 : 主页 > 网络编程 > PHP >

PHP数组与对象之间使用递归实现转换

来源:互联网 收集:自由互联 发布时间:2021-07-03
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; forea
 
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;
}
  
function object_to_array($e) { 
  $_arr = is_object($e) ? get_object_vars($e) : $e; 
  foreach ($_arr as $key => $val) { 
    $val = (is_array($val) || is_object($val)) ? object_to_array($val) : $val; 
    $arr[$key] = $val; 
  } 
  return $arr; 
}

网友评论