2009-12-25 107 views
36

我想将元素附加到关联数组的末尾。将值添加到PHP中的关联数组中

例如,我的数组是

$test=Array ([chemical] => asdasd [chemical_hazards] => ggggg) 

,我的结果应该是

$test=Array ([chemical] => asdasd [chemical_hazards] => ggggg [solution] => good) 

你能告诉我如何实现这一点?

回答

93

只需添加这就像你将与非关联数组:

$test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg'); //init 
$test['solution'] = 'good'; 
0

你可以用PHP的array_merge功能做到这一点。

$test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg'); 
$test2 = array('solution' => 'good'); 
$result = array_merge($test, $test2); 
var_dump($result);