2009-12-25 23 views
36

Desidero aggiungere un elemento alla fine di un array associativo.Aggiungere valori a un array associativo in PHP

Per esempio, la mia matrice è

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

e il mio risultato dovrebbe essere

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

Potrebbe dirmi come implementare questa?

risposta

93

Basta aggiungere che come si farebbe con una serie non associativa:

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

È possibile farlo con la funzione di PHP array_merge.

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