/ Published in: PHP
This function will convert any multidimensional array to an equivalent multidimensional object. It will recursively traverse the array to an infinite number of dimensions.
Expand |
Embed | Plain Text
Comments
Subscribe to comments
You need to login to post a comment.

You may also be able to get away with doing: $obj = (object)$arr; Or as a specific class: $obj = (YourClass)$array;
Hello
Good work it is very useful.
However, maybe the is a gap that could be a problem : when we have an array in a standard object, this array will not be converted because the script will call itself only if the container is an array.
So we can fix it as this :
[...] foreach ($array as $k => $v) { if (isarray($v)) $return->$k = $this->bindArrayToObject($v); else if (isobject($v)) $return->$k = $this->bindArrayToObject($v); // BUG FIX else $return->$k = $v; } [...]
Now, we analyse the objects too, and if they contains arrays, they will be converted too.
if (isarray($v) || isobject($v)) is better but I want to if (is_array($v)) the line of the bugfix :)
Hope it's help.
I want to "highlight" the line, sorry of the bad paste !