Objects and Arrays Conversion

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux