php array_merge 关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5233721/
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 array_merge associative arrays
提问by stephenbayer
I'm trying to prepend an item to the beginning of an associative array. I figured the best way to do this is to use array_merge, but I'm having some odd consequences. I get the id and Name of products from a mysql database, and it gets returned as an associative array, like this (not the actual data coming back, but sample data for this question that represents what the data looks like approximately):
我试图在关联数组的开头添加一个项目。我认为最好的方法是使用 array_merge,但我遇到了一些奇怪的后果。我从 mysql 数据库中获取产品的 id 和名称,并将其作为关联数组返回,如下所示(不是返回的实际数据,而是该问题的示例数据,表示数据的大致情况):
$products = array (1 => 'Product 1', 42 => 'Product 42', 100 => 'Product 100');
this is getting sent to an html helper to create a dropdown that associates the key with the value, and the value of the array item gets set as the text in the drop down select control. I need the first item to be something like "Please Select" with a key of 0, so I did this:
这将被发送到一个 html 帮助程序以创建一个将键与值关联的下拉列表,并且数组项的值被设置为下拉选择控件中的文本。我需要第一个项目类似于“请选择”,键为 0,所以我这样做了:
$products = array_merge(array(0 => "Select a product" ), $products);
The resulting array looks like this:
结果数组如下所示:
array(
0 => 'Select a product',
1 => 'Product 1',
2 => 'Product 42',
3 => 'Product 100'
);
when What I really wanted was not to lose the keys of the associative array. I was told that you can properly use array_merge with associative arrays in the manner I tried, however, I believe because my keys are intsthat it is not treating the array as a true associative array, and compressing them as illustrated above.
当我真正想要的是不要丢失关联数组的键时。有人告诉我,您可以按照我尝试的方式正确地将 array_merge 与关联数组一起使用,但是,我相信因为我的键是整数,所以它不会将数组视为真正的关联数组,并如上所示压缩它们。
The question is: Why is the array_merge function changing the keys of the items? can I keep it from doing this? OR is there another way for me to accomplish what I'm trying to do, to add the new item at the beginning of the array?
问题是:为什么array_merge函数会改变items的key?我可以阻止它这样做吗?或者还有另一种方法可以让我完成我想要做的事情,在数组的开头添加新项目?
回答by Mark Elliot
From the docs:
从文档:
If you want to append array elements from the second array to the first array while not overwriting the elements from the first array and not re-indexing, use the + array union operator
如果要将第二个数组中的数组元素附加到第一个数组,同时不覆盖第一个数组中的元素并且不重新索引,请使用 + 数组联合运算符
The keys from the first array argument are preserved when using the +
union operator, so reversing the order of your arguments and using the union operator should do what you need:
使用+
联合运算符时会保留第一个数组参数的键,因此颠倒参数的顺序并使用联合运算符应该可以满足您的需求:
$products = $products + array(0 => "Select a product");
回答by Mark Baker
Just for the fun of it
就是图个好玩儿
$newArray = array_combine(array_merge(array_keys($array1),
array_keys($array2)
),
array_merge(array_values($array1),
array_values($array2)
)
);
回答by vbence
array_merge
will recalculate numeric indexes. Because your associative array iuses numeric indexes they will get renumbered. You either insert a non-numeric charadter in front of the indices like:
array_merge
将重新计算数字索引。因为您的关联数组使用数字索引,所以它们将被重新编号。您可以在索引前插入一个非数字字符,例如:
$products = array ('_1' => 'Product 1', '_42' => 'Product 42', '_100' => 'Product 100');
Or you can create the resulting array manually:
或者您可以手动创建结果数组:
$newproducts = array (0 => "Select a product");
foreach ($products as $key => $value)
$newproducts[$key] = $value;
回答by Bob Fanger
You could use array operator: +
您可以使用数组运算符:+
$products = array(0 => "Select a product" ) + $products;
it will do a union and only works when the keys don't overlap.
它将进行联合并且仅在键不重叠时才起作用。
回答by Daniel Dinu
回答by Belinda
You could try something like
你可以尝试类似的东西
$products[0]='Select a Product'
ksort($products);
That should put the 0 at the start of the array but it will also sort the other products in numeric order which you may not want.
这应该将 0 放在数组的开头,但它还会按您可能不想要的数字顺序对其他产品进行排序。
回答by davisca
You man want to look at array_replace
function.
你这个人想看看array_replace
功能。
In this example they are function the same:
在这个例子中,它们的功能相同:
$products1 = array (1 => 'Product 1', 42 => 'Product 42', 100 => 'Product 100');
$products2 = array (0 => 'Select a product');
$result1 = array_replace($products1, $products2);
$result2 = $products1 + $products2;
Result for both result1 and result2: Keys are preserved:
array(4) {
[1] => string(9) "Product 1"
[42] => string(10) "Product 42"
[100] => string(11) "Product 100"
[0] => string(16) "Select a product"
}
However they differ if the same key is present in both arrays: + operator does not overwrite the value, array_replace does.
但是,如果两个数组中都存在相同的键,则它们会有所不同:+ 运算符不会覆盖该值,而 array_replace 会。