44

I want to append an element to to end of an associative array.

For example, my array is

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

and my result should be

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

Could you tell me how to implement this?

2 Answers 2

107

Just add it like you would with a non-associative array:

$test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg'); //init
$test['solution'] = 'good';
Sign up to request clarification or add additional context in comments.

Comments

4

You can do this with PHP's array_merge function.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.