Array_merge与+ [英] Array_merge versus +

查看:212
本文介绍了Array_merge与+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用 array_merge()关联数组我得到我想要的东西,但是当我用数字键阵列使用这些按键得到改变。

When I use array_merge() with associative arrays I get what I want, but when I use them with numerical key arrays the keys get changed.

使用 + 键是preserved,但它不与关联数组工作。

With + the keys are preserved but it doesn't work with associative arrays.

我不明白这是如何工作的,任何人可以向我解释?

I don't understand how this works, can anybody explain it to me?

推荐答案

由于这两个数组是数字索引,只有第一个数组中的值会被使用。

Because both arrays are numerically-indexed, only the values in the first array will be used.

+运算返回附加到左手阵列右手阵列; 作为同时存在于两个阵列密钥,从左侧数组中的元素将被使用,并且从右侧阵列的匹配元素将被忽略。

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

<一个href=\"http://php.net/manual/en/language.operators.array.php\">http://php.net/manual/en/language.operators.array.php

array_merge()稍有不同的行为:

如果输入的数组中有相同的字符串键,那么该键的后面的值将覆盖previous之一。 如果,但是,数组包含数字键,后面的值将不会覆盖原来的值,而是会被追加。用数字键输入数组中值将递增键从零的结果数组中开始重新编号。

If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will not overwrite the original value, but will be appended. Values in the input array with numeric keys will be renumbered with incrementing keys starting from zero in the result array.

<一个href=\"http://php.net/manual/en/function.array-merge.php\">http://php.net/manual/en/function.array-merge.php

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

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