php array_merge 关联数组

发布于 2024-10-21 00:22:17 字数 825 浏览 4 评论 0原文

我正在尝试将一个项目添加到关联数组的开头。我认为最好的方法是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

征﹌骨岁月お 2024-10-28 00:22:17

来自 文档

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

使用 + 联合运算符,因此颠倒参数的顺序并使用联合运算符应该可以满足您的需要:

$products = $products + array(0 => "Select a product");

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");
梦情居士 2024-10-28 00:22:17

只是为了它的乐趣

$newArray = array_combine(array_merge(array_keys($array1),
                                      array_keys($array2)
                                     ),
                          array_merge(array_values($array1),
                                      array_values($array2)
                                     )
                         );

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)
                                     )
                         );
帅冕 2024-10-28 00:22:17

array_merge 将重新计算数字索引。因为您的关联数组使用数字索引,所以它们将被重新编号。您可以在索引前面插入一个非数字字符,例如:

$products = array ('_1' => 'Product 1', '_42' => 'Product 42', '_100' => 'Product 100');

或者您可以手动创建结果数组:

$newproducts = array (0 => "Select a product");
foreach ($products as $key => $value)
    $newproducts[$key] = $value;

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:

$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;
幻想少年梦 2024-10-28 00:22:17

您可以使用 数组运算符+

$products = array(0 => "Select a product" ) + $products;

它会进行并集,并且仅在键不重叠时才有效。

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.

半寸时光 2024-10-28 00:22:17

来自 文档

带有数字键的输入数组中的值将使用递增键重新编号
结果数组中从零开始。

From the docs:

Values in the input array with numeric keys will be renumbered with incrementing keys
starting from zero in the result array.

浅听莫相离 2024-10-28 00:22:17

你想看看 array_replace功能。

在此示例中,它们的功能相同:

$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"
}

但是,如果两个数组中存在相同的键,则它们会有所不同:+ 运算符不会覆盖该值,而 array_replace 会覆盖该值。

You man want to look at array_replace function.

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.

嗳卜坏 2024-10-28 00:22:17

您可以尝试类似的方法

$products[0]='Select a Product'
ksort($products);

,应该将 0 放在数组的开头,但它也会按您可能不想要的数字顺序对其他产品进行排序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文