将项目推送到 PHP 中的关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3206020/
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
Push item to associative array in PHP
提问by ryudice
I've been trying to push an item to an associative array like this:
我一直在尝试将项目推送到这样的关联数组:
$new_input['name'] = array(
'type' => 'text',
'label' => 'First name',
'show' => true,
'required' => true
);
array_push($options['inputs'], $new_input);
However, instead of 'name' as the key in adds a number. Is there another way to do it?
但是,代替 'name' 作为键添加了一个数字。还有另一种方法吗?
回答by webbiedave
$options['inputs']['name'] = $new_input['name'];
回答by Murtaza Khursheed Hussain
Instead of array_push(), use array_merge()
而不是array_push(),使用array_merge()
It will merge two arrays and combine their items in a single array.
它将合并两个数组并将它们的项合并到一个数组中。
Example Code-
示例代码-
$existing_array = array('a'=>'b', 'b'=>'c');
$new_array = array('d'=>'e', 'f'=>'g');
$final_array=array_merge($existing_array, $new_array);
Its returns the resulting array in the final_array. And results of resulting array will be -
它在 final_array 中返回结果数组。结果数组的结果将是 -
array('a'=>'b', 'b'=>'c','d'=>'e', 'f'=>'g')
Please review this link, to be aware of possible problems.
请查看此链接,以了解可能存在的问题。
回答by Ajmal Salim
This is a cool function
这是一个很酷的功能
function array_push_assoc($array, $key, $value){
$array[$key] = $value;
return $array;
}
Just use
只需使用
$myarray = array_push_assoc($myarray, 'h', 'hello');
回答by Curtis
WebbieDave's solution will work. If you don't want to overwrite anything that might already be at 'name', you can also do something like this:
WebbieDave 的解决方案将起作用。如果您不想覆盖任何可能已经在 'name' 处的内容,您还可以执行以下操作:
$options['inputs']['name'][] = $new_input['name'];
$options['inputs']['name'][] = $new_input['name'];
回答by thetaiko
If $new_inputmay contain more than just a 'name' element you may want to use array_merge.
如果$new_input可能不仅仅包含您可能想要使用的“名称”元素array_merge。
$new_input = array('name'=>array(), 'details'=>array());
$new_input['name'] = array('type'=>'text', 'label'=>'First name'...);
$options['inputs'] = array_merge($options['inputs'], $new_input);
回答by Steven H
Curtis's answer was very close to what I needed, but I changed it up a little.
柯蒂斯的回答非常接近我所需要的,但我稍微改变了它。
Where he used:
他使用的地方:
$options['inputs']['name'][] = $new_input['name'];
I used:
我用了:
$options[]['inputs']['name'] = $new_input['name'];
Here's my actual code using a query from a DB:
这是我使用来自数据库的查询的实际代码:
while($row=mysql_fetch_array($result)){
$dtlg_array[]['dt'] = $row['dt'];
$dtlg_array[]['lat'] = $row['lat'];
$dtlg_array[]['lng'] = $row['lng'];
}
Thanks!
谢谢!
回答by ebnibrahem
i use php5.6
我用 php5.6
code:
代码:
$person = ["name"=>"mohammed", "age"=>30];
$person['addr'] = "Sudan";
print_r($person)
output
输出
Array( ["name"=>"mohammed", "age"=>30, "addr"=>"Sudan"] )
回答by vineet
Just change few snippet(use array_merge function):-
只需更改几个片段(使用 array_merge 函数):-
$options['inputs']=array_merge($options['inputs'], $new_input);
回答by Ryan Kinal
$new_input = array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true);
$options['inputs']['name'] = $new_input;
回答by Henry
There is a better way to do this:
有一个更好的方法来做到这一点:
If the array $arr_options contains the existing array.
如果数组 $arr_options 包含现有数组。
$arr_new_input['name'] = [
'type' => 'text',
'label' => 'First name',
'show' => true,
'required' => true
];
$arr_options += $arr_new_input;
Warning: $arr_options must exist. if $arr_options already has a ['name'] it wil be overwritten.
警告:$arr_options 必须存在。如果 $arr_options 已经有 ['name'] 它将被覆盖。
Hope this helps.
希望这可以帮助。

