Maison >développement back-end >tutoriel php >php函数实现对象与数组的相互转化
数组转化为对象(如果是多维数组的话递归):
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; }