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

时间:2011-12-14 22:46:58

标签: php arrays multidimensional-array array-merge

好的,这是我的第一个阵列:

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

这可能吗......

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

如果您只想将第二个数组附加到第一个数组(http://php.net/manual/en/function.array-push.php):

 array_splice($first, count($first), 0, $second);

答案 2 :(得分:0)

迭代第一组键,然后使用Union array operator应该可以解决问题。

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

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

    $first[$key] += $value;
}