Home  >  Article  >  Backend Development  >  How many layers does Torchlight have? PHP simple object and array conversion function code PHP multi-layer array and object conversion

How many layers does Torchlight have? PHP simple object and array conversion function code PHP multi-layer array and object conversion

WBOY
WBOYOriginal
2016-07-29 08:45:12932browse

Copy code The code is as follows:


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


The above content comes from cnblogs jaiho
php multi-layer array and object conversion
The purpose of multi-layer array and object conversion is very simple, which is convenient for processing multi-layer arrays and objects in WebService Conversion
Simple (array) and (object) can only handle single-level data, and are unable to convert multi-level arrays and objects.
The object can be converted into an array at one time through json_decode(json_encode($object), but there will be problems when encountering non-utf-8 encoded non-ascii characters in the object, such as gbk Chinese, not to mention the performance of json_encode and decode is also worthwhile Doubts.
The code below:

Copy the code The code is as follows:


function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
function arrayToObject($d) {
if (is_array($ d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return (object) array_map(__FUNCTION__, $d);
}
else {
/ / Return object
return $d;
}
}
// Useage:
// Create new stdClass Object
$init = new stdClass;
// Add some test data
$init->foo = "Test data" ;
$init->bar = new stdClass;
$init->bar->baaz = "Testing";
$init->bar->fooz = new stdClass;
$init->bar ->fooz->baz = "Testing again";
$init->foox = "Just test";
// Convert array to object and then object back to array
$array = objectToArray($init);
$object = arrayToObject($array);
// Print objects and array
print_r($init);
echo "n";
print_r($array);
echo "n";
print_r($object);
?>


The above introduces the conversion function code of how many layers of PHP simple objects and arrays there are in Torch Light, and the conversion of PHP multi-layer arrays and objects, including how many layers there are in Torch Light. I hope that friends who are interested in PHP tutorials can helped.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn