Recursive implementation of the conversion between the PHP array and the object, with the array recursion
The example in this article describes the use of recursive implementations of PHP arrays with objects. Share to everyone for your reference. The implementation method is as follows:
Here are some simple questions about the conversion of objects to arrays, using recursion to write two methods 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;}
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; }
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1021081.html www.bkjia.com true http://www.bkjia.com/PHPjc/1021081.html techarticle The recursive implementation of the PHP array with the object of the conversion method, the array recursive example of this article describes the PHP array and objects using recursive implementation of the method of conversion. Share to everyone for your reference ...