0

好的,这是我的第一个数组:

(
[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
            )

    )

这是我的第二个数组

(
[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
                    )
            )

基本上我需要的是第二个数组附加到第一个数组的末尾,第一个数组的键保留了键并将 step_number 更改为新键,因此最终键看起来像

(
[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)
....

但是当我这样做的时候

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

关键是

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

有没有可能做......

4

3 回答 3

2

尝试 array_merge_recursive .. http://php.net/array_merge_recursive

于 2011-12-14T22:49:12.727 回答
0

如果您只是想将第二个数组附加到第一个数组(http://php.net/manual/en/function.array-push.php)上,这也来自array_splice 的手册:

 array_splice($first, count($first), 0, $second);
于 2011-12-14T22:52:47.857 回答
0

迭代第一组键,然后使用Union array operator应该做的伎俩。

//$first = array(...);
//$second = array(...);

foreach ($second as $key => $value)
{
    if (!isset($first[$key]))
    {
        $first[$key] = array();
    }

    $first[$key] += $value;
}
于 2011-12-14T23:09:48.550 回答