That's not exactly what I was thinking :)  I was hoping there was something
built-in that I was missing :-/


On Fri, Feb 6, 2009 at 10:25 AM, Josh Team <josht...@gmail.com> wrote:

> function array2Obj($array, $obj = null)
>   {
>     if (is_null($obj))
>     {
>       $obj = new stdClass();
>     }
>
>     foreach ($array as $key => $value)
>     {
>       if (is_scalar($value))
>       {
>         $obj->$key = $value;
>       } else if (is_array($value)) {
>         if (isset($value[0]))
>         {
>           $obj->$key = new stdClass();
>           foreach ($value as $k => $v)
>           {
>             $obj->$key->$k = array2Obj($v);
>           }
>         } else {
>           $obj->$key = array2Obj($value);
>         }
>       }
>     }
>     return $obj;
>   }
>
>   $array = array(
>     'username' => 'jteam',
>     'password' => 'changeme',
>     '_explicitType' => 'as.class.user.as',
>     'Phonenumbers' => array(
>       0 => array(
>       '_explicitType' => 'as.class.phone.as',
>         'phonenumber' => '615-513-9185'
>       ),
>       1 => array(
>         '_explicitType' => 'as.class.phone.as',
>         'phonenumber' => '615-313-7679'
>       )
>     ),
>     'Profile' => array(
>       '_explicitType' => 'as.class.profile.as',
>       'first_name' => 'Josh',
>       'last_name' => 'Team',
>     ),
>     'Friends' => array(
>       0 => array(
>         '_explicitType' => 'as.class.user.as',
>         'username' => 'user1',
>         'email' => 'us...@email.com'
>       ),
>       1 => array(
>         '_explicitType' => 'as.class.user.as',
>         'username' => 'user2',
>         'email' => 'us...@email.com'
>       ),
>       2 => array(
>         '_explicitType' => 'as.class.user.as',
>         'username' => 'user3',
>         'email' => 'us...@email.com'
>       )
>     )
>   );
>
>   $obj = array2Obj($array);
>   echo '<pre>';
>   print_r($obj);
>   echo '</pre>';
>
> On Fri, Feb 6, 2009 at 11:24 AM, A.J. Brown <a...@ajbrown.org> wrote:
>
>> Hey guys,
>>
>> This is probably more approriate for the PHP list, but I'm not subscribed
>> with this email address, so I'll pose it here.
>>
>> Is there a better / built in way to turn an array into a stdClass?
>>
>> //TODO (is there / why isn't there) a built in way to do this?
>> $oTrack = new stdClass();
>> foreach( $trackData as $key => $value ) {
>>               $oTrack->$key = $value;
>> }
>>
>>
>> I'm doing this because the user has the option of passing in either a
>> model (an object type), or an array of the data that would be in the model.
>> But, I don't want to have two different code paths for processing the data.
>>
>> --
>> A.J. Brown
>> web | http://ajbrown.org
>> phone | (937) 660-3969
>>
>
>


-- 
A.J. Brown
web | http://ajbrown.org
phone | (937) 660-3969

Reply via email to