简体   繁体   English

将键和值插入数组

[英]Insert key and value into array

I have this code 我有这个代码

$array = array('o' => 'one', 't' => 'three', 'f' => 'four');
array_push($array, array('s' => 'six'));
print_r($array);

that insert new key and value into the array , but when i print the new $array it returned this 将新的键和值插入到array中,但是当我打印新的$ array时,它返回了

Array ( [o] => one [t] => three [f] => four [0] => Array ( [s] => six ) )

i need to return like this 我需要这样回来

Array ( [o] => one [t] => three [f] => four [s] => six )

how to remove [0] => Array () from the array ? 如何从数组中删除[0] => Array ()

array_push is intended for lists. array_push用于列表。

$arr = array(5, 6, 7);
array_push($arr, 8); // array(5, 6, 7, 8);

You can add elements to arrays in many ways, this is one: 您可以通过多种方式向数组添加元素,这是一种:

$array = array('o' => 'one', 't' => 'three', 'f' => 'four');
$array["s"] = "six";

Here is another: 这是另一个:

$array = array_merge($array, array("s" => "six"));

PHP treats lists like array(1, 2, 3); PHP将列表array(1, 2, 3);一样对待array(1, 2, 3); differently than associative arrays like array("foo" => "bar"); 与诸如array("foo" => "bar");关联数组不同array("foo" => "bar"); . The differences are minor, but they show up with functions like array_push . 差异不大,但它们通过array_push函数出现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM