dot_0620 2011-12-14 22:46 采纳率: 100%
浏览 9

PHP array_merge没有完全按照我的需要去做

Ok so here is my first array:

(
[1] => Array
    (
        [27] => Array
            (
                [product_id] => 27
                [type] => hardware
                [step_number] => 1

            )

        [372] => Array
            (
                [product_id] => 372
                [type] => hardware
                [step_number] => 1

            )

        [92] => Array
            (
                [product_id] => 92
                [type] => hardware
                [step_number] => 1
            )

    )

[2] => Array
    (
        [335] => Array
            (
                [product_id] => 335
                [type] => hardware
                [step_number] => 2

            )

        [62] => Array
            (
                [product_id] => 62
                [type] => hardware
                [step_number] => 2
            )

        [356] => Array
            (
                [product_id] => 356
                [type] => hardware
                [step_number] => 2
            )

    )

and here is my second array

(
[1] => Array
    (
        [655] => Array
            (
                [product_id] => 655
                [type] => optional
                [step_number] => 1
            )

        [54] => Array
            (
                [product_id] => 54
                [type] => optional
                [step_number] => 1
            )

        [554] => Array
            (
                [product_id] => 554
                [type] => optional
                [step_number] => 1
            )
    )

[2] => Array
    (
        [33] => Array
            (
                [product_id] => 33
                [type] => optional
                [step_number] => 2
            )
        [612] => Array
            (
                [product_id] => 612
                [type] => optional
                [step_number] => 2
            )
        [5] => Array
            (
                [product_id] => 5
                [type] => optional
                [step_number] => 2
            )
    ) 

 [3] => Array
            (
                [444] => Array
                    (
                        [product_id] => 444
                        [type] => optional
                        [step_number] => 3
                    )
                [6] => Array
                    (
                        [product_id] => 6
                        [type] => optional
                        [step_number] => 3
                    )
                [53] => Array
                    (
                        [product_id] => 53
                        [type] => optional
                        [step_number] => 3
                    )
            )

Basically what i need is the second array appended to the end of the first array with the keys of the first array preserved the keys and changing the step_number to the new key so the final keys looks like

(
[1] => Array
(
        [27] => Array
            (
                [step_number] => 1)
....
[2] => Array
(
        [335] => Array
            (
                [step_number] => 2)
....
[3] => Array
(
        [655] => Array
            (
                [step_number] => 3)
....
[4] => Array
(
        [33] => Array
            (
                [step_number] => 4)
....
[5] => Array
(
        [444] => Array
            (
                [step_number] => 5)
....

but when i do

$all = array_merge($first, $second);

the keys are

(
[0] => Array
[1] => Array
[2] => Array
[3] => Array
[4] => Array

Is that possible to do....

  • 写回答

3条回答 默认 最新

  • doushi4633 2011-12-14 22:49
    关注

    Try array_merge_recursive .. http://php.net/array_merge_recursive

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)