Re: array to object conversion
| From: | Jochem Maas | Date: | Thu, 15 Dec 2005 11:03:11 +0000 |
| Subject: | Re: array to object conversion | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-227461@lists.php.net to get a copy of this message | ||
Cyril wrote:
Is there any way of converting an array to an object of a type/class other than stdClass?write a function? - bare in mind this is just an idea - it would need more work to make it usuable (and the code is completely untested): class Test {} function castFromArray($class, $arr) { $x = new $class; foreach ($arr as $k => $v) {$x->$k = $v;} return $x; } $t = castFromArray('Test', array("test1" => 123, "test2" => 345,
"test3" => "abc"));var_dump($t);
I have been using the new PDO system and have experimented with fetching rows with PDO::FETCH_CLASS I would quite like to do the same with a plain array. Any comments appreciated. Cyril