php array_merge 关联数组
我正在尝试将一个项目添加到关联数组的开头。我认为最好的方法是使用 array_merge,但我遇到了一些奇怪的后果。我从 mysql 数据库获取产品的 id 和名称,并将其作为关联数组返回,如下所示(不是返回的实际数据,而是代表数据大致样子的该问题的示例数据):
$products = array (1 => 'Product 1', 42 => 'Product 42', 100 => 'Product 100');
这是发送到 html 帮助程序以创建一个将键与值关联的下拉列表,并且数组项的值被设置为下拉选择控件中的文本。我需要第一个项目类似于“请选择”,键为 0,所以我这样做了:
$products = array_merge(array(0 => "Select a product" ), $products);
生成的数组如下所示:
array(
0 => 'Select a product',
1 => 'Product 1',
2 => 'Product 42',
3 => 'Product 100'
);
当我真正想要的不是丢失关联数组的键时。有人告诉我,您可以按照我尝试的方式正确地将 array_merge 与关联数组一起使用,但是,我相信因为我的键是 int,所以它不会将数组视为真正的关联数组,并压缩它们如上图所示。
问题是:为什么 array_merge 函数会更改项目的键?我可以阻止它这样做吗?或者还有另一种方法可以让我完成我想要做的事情,即在数组的开头添加新项目?
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):
$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:
$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 ints that it is not treating the array as a true associative array, and compressing them as illustrated above.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
来自 文档:
使用
+
联合运算符,因此颠倒参数的顺序并使用联合运算符应该可以满足您的需要:From the docs:
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:只是为了它的乐趣
Just for the fun of it
array_merge
将重新计算数字索引。因为您的关联数组使用数字索引,所以它们将被重新编号。您可以在索引前面插入一个非数字字符,例如:或者您可以手动创建结果数组:
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:Or you can create the resulting array manually:
您可以使用 数组运算符:
+
它会进行并集,并且仅在键不重叠时才有效。
You could use array operator:
+
it will do a union and only works when the keys don't overlap.
来自 文档:
From the docs:
你想看看
array_replace
功能。在此示例中,它们的功能相同:
但是,如果两个数组中存在相同的键,则它们会有所不同:+ 运算符不会覆盖该值,而 array_replace 会覆盖该值。
You man want to look at
array_replace
function.In this example they are function the same:
However they differ if the same key is present in both arrays: + operator does not overwrite the value, array_replace does.
您可以尝试类似的方法
,应该将 0 放在数组的开头,但它也会按您可能不想要的数字顺序对其他产品进行排序。
You could try something like
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.