将项目推入PHP中的关联数组

时间:2022-08-26 14:15:53

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”作为添加数字的键。还有别的办法吗?

10 个解决方案

#1


84  

$options['inputs']['name'] = $new_input['name'];

#2


46  

Instead of array_push(), use array_merge()

使用array_merge()代替array_push()

$existing_array = array('a'=>'b', 'b'=>'c');
$new_array = array('d'=>'e', 'f'=>'g');

array_merge($existing_array, $new_array);

Its returns the resulting array append in the initial array in this case $existing_array.

它返回初始数组中的结果数组追加,在本例中为$existing_array。

And resulting array will be

结果是数组

array('a'=>'b', 'b'=>'c','d'=>'e', 'f'=>'g')

Please review this link, to be aware of possible problems.

请查看此链接,以了解可能出现的问题。

#3


8  

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');

Credits & Explanation

学分和解释

#4


6  

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'];

选择美元[“输入”]['名字'][]= $ new_input(“名字”);

#5


3  

If $new_input may contain more than just a 'name' element you may want to use array_merge.

如果$new_input可能包含不止一个“name”元素,那么您可能需要使用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);

#6


3  

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:

下面是我使用DB查询的实际代码:

while($row=mysql_fetch_array($result)){ 
    $dtlg_array[]['dt'] = $row['dt'];
    $dtlg_array[]['lat'] = $row['lat'];
    $dtlg_array[]['lng'] = $row['lng'];
}

Thanks!

谢谢!

#7


2  

Just change few snippet(use array_merge function):-

只需更改几个代码片段(使用array_merge函数):-。

  $options['inputs']=array_merge($options['inputs'], $new_input);

#8


1  

$new_input = array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true);
$options['inputs']['name'] = $new_input;

#9


0  

You can try.

你可以试一试。

$options['inputs'] = $options['inputs'] + $new_input;

#10


0  

You can use array_merge($array1, $array2) to merge the associative array. Example:

可以使用array_merge($array1, $array2)合并关联数组。例子:

$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));

Output:

输出:

Array ( [0] => red [1] => green [2] => blue [3] => yellow )

#1


84  

$options['inputs']['name'] = $new_input['name'];

#2


46  

Instead of array_push(), use array_merge()

使用array_merge()代替array_push()

$existing_array = array('a'=>'b', 'b'=>'c');
$new_array = array('d'=>'e', 'f'=>'g');

array_merge($existing_array, $new_array);

Its returns the resulting array append in the initial array in this case $existing_array.

它返回初始数组中的结果数组追加,在本例中为$existing_array。

And resulting array will be

结果是数组

array('a'=>'b', 'b'=>'c','d'=>'e', 'f'=>'g')

Please review this link, to be aware of possible problems.

请查看此链接,以了解可能出现的问题。

#3


8  

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');

Credits & Explanation

学分和解释

#4


6  

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'];

选择美元[“输入”]['名字'][]= $ new_input(“名字”);

#5


3  

If $new_input may contain more than just a 'name' element you may want to use array_merge.

如果$new_input可能包含不止一个“name”元素,那么您可能需要使用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);

#6


3  

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:

下面是我使用DB查询的实际代码:

while($row=mysql_fetch_array($result)){ 
    $dtlg_array[]['dt'] = $row['dt'];
    $dtlg_array[]['lat'] = $row['lat'];
    $dtlg_array[]['lng'] = $row['lng'];
}

Thanks!

谢谢!

#7


2  

Just change few snippet(use array_merge function):-

只需更改几个代码片段(使用array_merge函数):-。

  $options['inputs']=array_merge($options['inputs'], $new_input);

#8


1  

$new_input = array('type' => 'text', 'label' => 'First name', 'show' => true, 'required' => true);
$options['inputs']['name'] = $new_input;

#9


0  

You can try.

你可以试一试。

$options['inputs'] = $options['inputs'] + $new_input;

#10


0  

You can use array_merge($array1, $array2) to merge the associative array. Example:

可以使用array_merge($array1, $array2)合并关联数组。例子:

$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));

Output:

输出:

Array ( [0] => red [1] => green [2] => blue [3] => yellow )