php array_merge 关联数组 [英] php array_merge associative arrays

查看:29
本文介绍了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);

结果数组如下所示:

数组(0 =>'选择一个产品',1 =>'产品 1',2 =>'产品 42',3 =>'产品 100');

当我真正想要的是不要丢失关联数组的键时.有人告诉我,您可以按照我尝试的方式正确地将 array_merge 与关联数组一起使用,但是,我相信因为我的键是 int,它不会将数组视为真正的关联数组,并对其进行压缩如上图所示.

问题是:为什么array_merge函数会改变items的key?我可以阻止它这样做吗?或者有没有另一种方法可以让我完成我想要做的事情,在数组的开头添加新项目?

解决方案

来自文档:><块引用>

如果您想将第二个数组中的数组元素追加到第一个数组,同时不覆盖第一个数组中的元素并且不重新索引,请使用 + 数组联合运算符

当使用 + 联合运算符时,第一个数组参数的键被保留,因此颠倒参数的顺序并使用联合运算符应该可以满足您的需要:

$products = $products + array(0 => "选择产品");

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?

解决方案

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

这篇关于php array_merge 关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆