Converting a PHP array to class variables.
Posted
by animuson
on Stack Overflow
See other posts from Stack Overflow
or by animuson
Published on 2010-04-26T17:24:09Z
Indexed on
2010/04/26
17:43 UTC
Read the original article
Hit count: 195
Simple question, how do I convert an associative array to variables in a class? I know there is casting to do an (object) $myarray
or whatever it is, but that will create a new stdClass and doesn't help me much. Are there any easy one or two line methods to make each $key => $value
pair in my array into a $key = $value
variable for my class? I don't find it very logical to use a foreach loop for this, I'd be better off just converting it to a stdClass and storing that in a variable, wouldn't I?
class MyClass {
var $myvar; // I want variables like this, so they can be references as $this->myvar
function __construct($myarray) {
// a function to put my array into variables
}
}
© Stack Overflow or respective owner