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
            )

    )

そして、これが私の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を新しいキーに変更して、最初の配列の最後に追加された2番目の配列で、最終的なキーは次のようになります

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

最初のキーのセットを繰り返し処理してから、Union array operatorshouldを使用するとうまくいきます。

//$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 に答える
0

2番目の配列を最初の配列に追加するだけの場合、これはarray_spliceのマニュアルにもあります(http://php.net/manual/en/function.array-push.php):

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