2009-12-25 24 views
36

Quiero agregar un elemento al final de una matriz asociativa.Agregar valores a una matriz asociativa en PHP

Por ejemplo, mi matriz es

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

y mi resultado debe ser

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

¿Me podría decir cómo implementar esto?

Respuesta

93

Sólo añadir que como lo haría con una matriz no asociativo:

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

Usted puede hacer esto con la función de PHP array_merge.

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