"Afghanistan", "ax" => "Γ…...">

How to combine variables into an array, keeping the variable name as a key

I have an array:

$countries = array( "af" => "Afghanistan", "ax" => "Γ…land Islands", "al" => "Albania", "dz" => "Algeria" ); 

and some variables:

 $as = "American Samoa"; $ad = "Andorra"; 

How to combine variables into an array, keeping the variable name as the key in the array?

+4
source share
2 answers

You can use compact to create an array from your variables, and then just add two arrays together:

 $countries += compact('as', 'ad'); 

Look at the action .

+9
source
 foreach(array('as','ad') as $name){ $countries[$name] = $$name; } 
0
source

All Articles