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 } }
To convert an array into the object, stdClass() is used. The stdClass() is an empty class, which is used to cast other types to object. If an object is converted to object, its not modified. But, if object type is converted/type-casted an instance of stdClass is created, if it is not NULL.
The var keyword in PHP is used to declare a property or variable of class which is public by default. The var keyword is same as public when declaring variables or property of a class.
Use print_r() Function to Echo or Print an Array in PHP It stores the output of print_r() function. If its value is True , then the function will return the value which is supposed to print.
It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.
This PHP extract function uses array keys as variable names and values as variable values. So using this extract function, we can print arrays by calling a variable name. From this example you will also see how to php array extract specific keys. It's a great feature of PHP. We can convert array to variables in PHP using this extract function.
Using a type casting method you can quickly carry out an array to StdClass conversion in one line of code. Alternatively, you can harness the power of PHP’s encoding functions such as json_encode and json_decode.
You know that PHP extract () function imports variables into the local symbol table from an array. This PHP extract function uses array keys as variable names and values as variable values. So using this extract function, we can print arrays by calling a variable name. From this example you will also see how to php array extract specific keys.
With the array, we first parse it into a JSON string with json_encode and finally, decode the string with the json_decode function. And again, we can utilise the object style syntax to access the values via their relative keys. And that is it, two super slick one-liners to convert an array into a fully operable StdClass object.
This simple code should work:
<?php class MyClass { public function __construct(Array $properties=array()){ foreach($properties as $key => $value){ $this->{$key} = $value; } } } ?>
Example usage
$foo = new MyClass(array("hello" => "world")); $foo->hello // => "world"
Alternatively, this might be a better approach
<?php class MyClass { private $_data; public function __construct(Array $properties=array()){ $this->_data = $properties; } // magic methods! public function __set($property, $value){ return $this->_data[$property] = $value; } public function __get($property){ return array_key_exists($property, $this->_data) ? $this->_data[$property] : null ; } } ?>
Usage is the same
// init $foo = new MyClass(array("hello" => "world")); $foo->hello; // => "world" // set: this calls __set() $foo->invader = "zim"; // get: this calls __get() $foo->invader; // => "zim" // attempt to get a data[key] that isn't set $foo->invalid; // => null
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With