2009-12-25 89 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); 
相關問題