Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个数组:
$countries = array( "af" => "Afghanistan", "ax" => "Åland Islands", "al" => "Albania", "dz" => "Algeria" );
和一些变量:
$as = "American Samoa"; $ad = "Andorra";
如何将变量组合到数组中,同时保持变量名作为数组中的键?
您可以使用compact从变量中创建一个数组,然后简单地将两个数组相加:
compact
$countries += compact('as', 'ad');
看到它在行动。
foreach(array('as','ad') as $name){ $countries[$name] = $$name; }