Use Recursive conversion between PHP arrays and objects. PHP array and object using Recursive conversion method this article describes the PHP array and object using Recursive conversion method. Share it with you for your reference. Implement recursive conversion between PHP arrays and objects
This article describes how to implement recursive conversion between PHP arrays and objects. Share it with you for your reference. The specific implementation method is as follows:
There are some simple conversions between objects and arrays. two methods are written recursively as follows:
?
1
2
3
4
5
6
7
8 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;
}
?
1
2
3
4
5
6
7
8
9 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;
}
?
1
2
3
4
5
6
7
8 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 will help you with php programming.
Examples in this article describes how to implement recursive conversion between PHP arrays and objects. Share it with you for your reference. Specific implementation...