2009-12-25 9 views
36

Je souhaite ajouter un élément à la fin d'un tableau associatif.Ajouter des valeurs à un tableau associatif dans PHP

Par exemple, mon tableau est

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

et mon résultat devrait être

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

Pourriez-vous me dire comment mettre en œuvre ce?

Répondre

93

Ajoutez juste comme vous le feriez avec un tableau non associatif:

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

Vous pouvez le faire avec la fonction de PHP array_merge.

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