php PHP如何添加到数组的末尾?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6797641/
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
PHP How to add to end of array?
提问by vitalyp
I'm having difficulty adding to the end of an array. Not sure how to do it. Please help.
我很难添加到数组的末尾。不知道该怎么做。请帮忙。
$person = array();
$person = array("name"=>"tom", "age"=>20, "height"=>180);
How do I add to the end of an array? I want to add "weight"=>120 to the end of an existing array.
如何添加到数组的末尾?我想将 "weight"=>120 添加到现有数组的末尾。
Thanks
谢谢
回答by Brad
Since this is an associative array, just do:
由于这是一个关联数组,只需执行以下操作:
$person['weight']=120;
For regular numerically indexed arrays, you can use array_push()
or $person []= "new value";
.
对于常规的数字索引数组,您可以使用array_push()
或$person []= "new value";
。