object to array
Moderator: General Moderators
object to array
is there a function that takes an object and returns it's member variables in an array?
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
The property names? or the property values?
Code: Select all
[feyd@home]>php -r "class foo{public $someVar; private $someOtherVar; public function __construct(){$this->someVar = mt_rand(1,100); $this->someOtherVar = mt_rand(100,200);}} $foo = new foo(); var_dump($foo, (array)$foo);"
object(foo)#1 (2) {
["someVar"]=>
int(20)
["someOtherVar:private"]=>
int(135)
}
array(2) {
["someVar"]=>
int(20)
[" foo someOtherVar"]=>
int(135)
}All I can think of is serializing.. and then writing your own unserialize function.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Code: Select all
$array = get_object_vars($obj);I ended up using object_get_vars....
Code: Select all
public function loadFromArray($array){
$object_vars = get_object_vars($this);
$values = array_intersect_key($array, $object_vars);
foreach($values as $key => $val){
$this->{$key} = $val;
}
}