将值添加到 PHP 中的关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1960730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:27:26 来源:igfitidea点击:
Add values to an associative array in PHP
提问by Thinker
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?
你能告诉我如何实现这个吗?
回答by Sasha Chedygov
Just add it like you would with a non-associative array:
只需像使用非关联数组一样添加它:
$test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg'); //init
$test['solution'] = 'good';
回答by T. Gungordu
You can do this with PHP's array_mergefunction.
您可以使用 PHP 的array_merge函数执行此操作。
$test = array('chemical' => 'asdasd', 'chemical_hazards' => 'ggggg');
$test2 = array('solution' => 'good');
$result = array_merge($test, $test2);
var_dump($result);

