array_merge导致不同的键[重复]



此问题已经存在 这里有一个答案:</ p>





  • 带有数字键的PHP array_merge



    8 answers

    </ span>

    </ li>

    </ ul>

    </ div>

    我有2个数组。 我希望将它们组合起来。</ p>

    数组1:</ strong> </ p>

     数组

    [100] =&gt;
    [50] =&gt;
    [CREDIT] =&gt; 14.31
    [CHECK] =&gt;

    </ code> </ pre>

    数组2:</ strong> </ p>

     数组

    [id] =&gt; 491
    [ce_unique_id] =&gt; CE144915960926-21
    [company_id ] =&gt; 1

    </ code> </ pre>

    我在将它们与 array_merge()</ code>合并后得到这个结果:</ p> \ n

      Array 

    ---&gt; [0] =&gt;
    ---&gt; [1] =&gt;
    [CREDIT] =&gt; 14.31
    [CHECK] =&gt;
    [id] =&gt; 491
    [ce_unique_id] =&gt; CE144915960926-21
    [company_id] =&gt; 1

    </ code> </ pre>

    我的预期结果是:</ p>

      Array 

    ---&gt; [100] =&gt;
    ---&gt; [ 50] =&gt;
    [CREDIT] =&gt; 14.31
    [CHECK] =&gt;
    [id] =&gt; 491
    [ce_unique_id] =&gt; CE144915960926-21
    [company_id] =&gt; 1

    </ code> </ pre>

    为什么合并2个数组后数字键会被更改?</ p>
    </ div>

展开原文

原文

This question already has an answer here:

I have 2 arrays. I want to combine them.

array 1:

Array
(
[100] => 
[50] => 
[CREDIT] => 14.31
[CHEQUE] => 
)

array 2:

Array
(
[id] => 491
[ce_unique_id] => CE144915960926-21
[company_id] => 1
)

I am getting this result after merging them with array_merge():

Array
(
--->[0] => 
--->[1] => 
[CREDIT] => 14.31
[CHEQUE] => 
[id] => 491
[ce_unique_id] => CE144915960926-21
[company_id] => 1
)

And my expected result is:

Array
(
--->[100] => 
--->[50] => 
[CREDIT] => 14.31
[CHEQUE] => 
[id] => 491
[ce_unique_id] => CE144915960926-21
[company_id] => 1
)

Why the numeric keys are getting changed after merging 2 arrays?

</div>

1个回答



array_merge </ code>以不同方式处理数字键 http://php.net/manual/en/function.array-merge.php </ p>

但是 上面的文档表明使用以下内容合并应该有效:</ p>

  $ newArray = $ array1 + $ array2; 
</ code> </ pre>
</ div >

展开原文

原文

array_merge treats numeric keys differently http://php.net/manual/en/function.array-merge.php

However the documentation above suggests that merging using the following should work:

$newArray = $array1 + $array2;

dongmei8760
dongmei8760 有用!。 谢谢。
大约 6 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐