Re: Objects and Arrays Conversion

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

 



VamVan wrote:
> 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
>
>   
This page at the bottom describes your array_2_object function as a
simple typecast:
http://us2.php.net/manual/en/language.types.object.php
$object = (object) $array;

As for the object to array, the same thing applies:
http://us2.php.net/manual/en/language.types.array.php

$array = (array) $object;

Not sure if these are PHP 5 only or not.

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[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