-- A.J. Brown <[email protected]> wrote
(on Friday, 06 February 2009, 10:24 AM -0700):
> 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.

Just cast the array to an object:

    $oTrack = (object) $trackData;

This creates a new stdClass object with the key/value pairs of the
array as instance properties.

-- 
Matthew Weier O'Phinney
Software Architect       | [email protected]
Zend Framework           | http://framework.zend.com/

Reply via email to