Created
December 15, 2017 07:26
-
-
Save yusureabc/18e9f7bc73f5b77b60e6c38505c85c6d to your computer and use it in GitHub Desktop.
数组对象互转
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 数组转换对象 | |
* | |
* @param $e 数组 | |
* @return object|void | |
*/ | |
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 对象转换数组 | |
* | |
* @param $e StdClass对象实例 | |
* @return array|void | |
*/ | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment