php - Variable name set to key and its value set to value in an array
431
I want to set up a load of variables then loop though them adding them to an array with the key set as the variable name, something like below.
I am just not sure how you create the loop.
// create an array
$array = array();
// Set up Variable
$green = "31d944";
$red = "d92929";
$blue = "1b43d9";
$pink = "d96ad8";
$orange = "d98225";
// Loop though - not sure how this would work...
for($i=0; $i<5; $i++){
$array[] = $varNmae => $green;
}
// Output something like below
$array['green']; // output '31d944'
$array['red']; // output 'd92929'
$array['blue']; // output '1b43d9'
$array['pink']; // output 'd96ad8'
$array['orange']; // output 'd98225'
Answer
Solution:
is the opposite of
(extracting an associate array into the local symbol table), and is what you want to use.
CodePad.
Answer
Solution:
The function
does exactly this:
Answer
Solution:
What is the thinking in defining the variables and then adding them to an array, instead of just initialising the array?
Could be looped with
Or accessed as