I want to create separate variables from the key of an array where variable is the key and the content is the value of that key in the array
5 Answers
Example
<?php
$array = array('a' => 'abc', 'b' => 'def');
extract($array);
var_dump($a, $b);
// string(3) "abc"
// string(3) "def"
1 Comment
Gordon
+1 for being the first to supply a working solution along with an example.
Use the extract() function for this.
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array);
which will give:
$color = 'blue'
$size = 'medium'
$shape = 'sphere'
extractfor this, be aware that the default mode isEXTR_OVERWRITEwhich poses the potential security risk of overwriting important existing variables. It is safer to useEXTR_PREFIX_ALLor not to useextractat all