dsa5211314 2012-03-25 19:02
浏览 80
已采纳

为什么array_merge_recursive像这样工作?

I have 3 arrays like this:

1st

Array
(
    [0695] => Array
        (
            [loan_number] => 0695
            [start_date] => 2011-12-25
        )
)

2nd

Array
(
    [0695] => Array
        (
            [name] => ''
            [address] => ''
        )
)

3rd

Array
(
    [0695] => Array
        (
            [items] => Array
                (
                    [0] => Array
                        (
                            [item] => ''
                            [qty] => ''
                            [price] => ''
                        )
                )
        )
)

array_merge_recursive(array1,array2,array3) produces:

Array
(
    [0695] => Array
        (
            [loan_number] => 0695
            [start_date] => 2011-12-25
            [name] => ''
            [address] => ''
            [items] => Array
                (
                    [0] => Array
                        (
                            [item] => ''
                            [qty] => ''
                            [price] => ''
                        )
                )
        )
)

but if the array keys are like this

Array
(
    [1685] => Array
        (
            [loan_number] => 1685
            [start_date] => 2011-12-25
        )
)

Array
(
    [1685] => Array
        (
            [name] => ''
            [address] => ''
        )
)

Array
(
    [1685] => Array
        (
            [items] => Array
                (
                    [0] => Array
                        (
                            [item] => ''
                            [qty] => ''
                            [price] => ''
                        )
                )
        )
)

this produce result like this

Array
(
    [0] => Array
        (
            [loan_number] => 1685
            [start_date] => 2011-12-25
        )

    [1] => Array
        (
            [name] => ''
            [address] => ''
        )

    [2] => Array
        (
            [items] => Array
                (
                    [0] => Array
                        (
                            [item] => ''
                            [qty] => ''
                            [price] => ''
                        )
                )
        )
)

Any idea why this happening and how to prevent this and get the result as first one?

  • 写回答

1条回答 默认 最新

  • drpkcwwav20524605 2012-03-25 19:10
    关注

    From the documentation:

    If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

    "0695" is not a numeric key, but "1685" is.

    The first comment (in fact most of the comments) on that page describes a workaround.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组