Sometimes it’s just easier to work with an Array instead of a StdObject. Converting an array back to a StdObject is also easy to do.
How To Convert Object To Array
function object2array($data) { if(!is_object($data) && !is_array($data)) return $data; if(is_object($data)) $data = get_object_vars($data); return array_map('object2array', $data); }
How To Convert Array To Object
function array2object($data) { if(!is_array($data)) return $data; $object = new stdClass(); if (is_array($data) && count($data) > 0) { foreach ($data as $name=>$value) { $name = strtolower(trim($name)); if (!empty($name)) { $object->$name = array2object($value); } } } return $object; }
Ashish Kumar says
Useful post, I am working on PHP technology so this blog is proof to be great for me!!!!!!!! thanks for sharing it.