Objects and Arrays Conversion

From: Date: Wed, 03 Dec 2008 01:41:06 +0000
Subject: Objects and Arrays Conversion
Groups: php.general 
Request: Send a blank email to php-general+get-284025@lists.php.net to get a copy of this message
Hello All, I was stuck with this issue. So just felt the need to reach out to other strugglers. May be people might enjoy this: Here is the code for object to array and array to object conversion: function object_2_array($data) { if(is_array($data) || is_object($data)) { $result = array(); foreach ($data as $key => $value) { $result[$key] = object_2_array($value); } return $result; } return $data; } function array_2_object($array) { $object = new stdClass(); if (is_array($array) && count($array) > 0) { foreach ($array as $name=>$value) { $name = strtolower(trim($name)); if (!empty($name)) { $object->$name = $value; } } } return $object; } have fun !!!!! Thanks

« previous php.general (#284025) next »