0

現在、そのように見える2つの配列があり、グループ化/マージしようとしましたが、うまくいきませんでした。

$array1;

Array
(
    [1] => Array
        (
            [options] => 1
            [barcode] => 
            [supplier] => 10
            [topcat] => Fibre,1
            [cat] => Acrylic,1
            [range] => Clearance Chenille,14
            [colour] => 
            [type] => Ball
            [option] => Array
                (
                    [1] => Array
                        (
                            [type] => Ball
                            [barcode] => 
                            [length] => 
                            [wpi] => 
                            [dyeable] => 
                            [feltable] => 
                            [sold_weight] => 
                            [gauge] => 
                            [price] => 
                            [pack_qty] => 
                            [shipping_weight] => 
                            [cost_price] => 
                        )

                )

        )

    [2] => Array
        (
            [options] => 1
            [barcode] => 
            [supplier] => 10
            [topcat] => Fibre,1
            [cat] => Acrylic,1
            [range] => Clearance Chenille,14
            [colour] => 
            [type] => Ball
            [option] => Array
                (
                    [1] => Array
                        (
                            [type] => Ball
                            [barcode] => 
                            [length] => 
                            [wpi] => 
                            [dyeable] => 
                            [feltable] => 
                            [sold_weight] => 
                            [gauge] => 
                            [price] => 
                            [pack_qty] => 
                            [shipping_weight] => 
                            [cost_price] => 
                        )

                    [2] => Array
                        (
                            [type] => Ball
                            [barcode] => 
                            [length] => 
                            [wpi] => 
                            [dyeable] => 
                            [feltable] => 
                            [sold_weight] => 
                            [gauge] => 
                            [price] => 
                            [pack_qty] => 
                            [shipping_weight] => 
                            [cost_price] => 
                        )

                )

        )

)

$array2;

Array
(
    [1] => Array
        (
            [1] => Array
                (
                    [0] => Array
                        (
                            [name] => 500aqua.jpg
                            [type] => image/jpeg
                            [tmp_name] => C:\xampp\tmp\php6C70.tmp
                            [error] => 0
                            [size] => 133659
                        )

                )

        )

    [2] => Array
        (
            [1] => Array
                (
                    [0] => Array
                        (
                            [name] => 500beige.jpg
                            [type] => image/jpeg
                            [tmp_name] => C:\xampp\tmp\php6C71.tmp
                            [error] => 0
                            [size] => 148940
                        )

                    [1] => Array
                        (
                            [name] => 500beige-zoom.jpg
                            [type] => image/jpeg
                            [tmp_name] => C:\xampp\tmp\php6C72.tmp
                            [error] => 0
                            [size] => 211420
                        )

                )

            [2] => Array
                (
                    [0] => Array
                        (
                            [name] => 500beige.jpg
                            [type] => image/jpeg
                            [tmp_name] => C:\xampp\tmp\php6C83.tmp
                            [error] => 0
                            [size] => 148940
                        )

                    [1] => Array
                        (
                            [name] => 500beige-zoom.jpg
                            [type] => image/jpeg
                            [tmp_name] => C:\xampp\tmp\php6C84.tmp
                            [error] => 0
                            [size] => 211420
                        )

                )

        )

)

私はそれらを次のようにマージしようとしています:

Array
(
    [1] => Array
        (
            [options] => 1
            [barcode] => 
            [supplier] => 10
            [topcat] => Fibre,1
            [cat] => Acrylic,1
            [range] => Clearance Chenille,14
            [colour] => 
            [type] => Ball
            [option] => Array
                (
                    [1] => Array
                        (
                            [type] => Ball
                            [barcode] => 
                            [length] => 
                            [wpi] => 
                            [dyeable] => 
                            [feltable] => 
                            [sold_weight] => 
                            [gauge] => 
                            [price] => 
                            [pack_qty] => 
                            [shipping_weight] => 
                            [cost_price] => 
                            [files] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => 500aqua.jpg
                                            [type] => image/jpeg
                                            [tmp_name] => C:\xampp\tmp\php6C70.tmp
                                            [error] => 0
                                            [size] => 133659
                                        )
                                )
                        )
                )
        )
    [2] => Array
        (
            [options] => 1
            [barcode] => 
            [supplier] => 10
            [topcat] => Fibre,1
            [cat] => Acrylic,1
            [range] => Clearance Chenille,14
            [colour] => 
            [type] => Ball
            [option] => Array
                (
                    [1] => Array
                        (
                            [type] => Ball
                            [barcode] => 
                            [length] => 
                            [wpi] => 
                            [dyeable] => 
                            [feltable] => 
                            [sold_weight] => 
                            [gauge] => 
                            [price] => 
                            [pack_qty] => 
                            [shipping_weight] => 
                            [cost_price] => 
                            [files] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => 500beige.jpg
                                            [type] => image/jpeg
                                            [tmp_name] => C:\xampp\tmp\php6C71.tmp
                                            [error] => 0
                                            [size] => 148940
                                        )
                                    [1] => Array
                                        (
                                            [name] => 500beige-zoom.jpg
                                            [type] => image/jpeg
                                            [tmp_name] => C:\xampp\tmp\php6C72.tmp
                                            [error] => 0
                                            [size] => 211420
                                        )
                                )
                        )
                    [2] => Array
                        (
                            [type] => Ball
                            [barcode] => 
                            [length] => 
                            [wpi] => 
                            [dyeable] => 
                            [feltable] => 
                            [sold_weight] => 
                            [gauge] => 
                            [price] => 
                            [pack_qty] => 
                            [shipping_weight] => 
                            [cost_price] => 
                            [files] => Array
                                (
                                    [0] => Array
                                        (
                                            [name] => 500beige.jpg
                                            [type] => image/jpeg
                                            [tmp_name] => C:\xampp\tmp\php6C83.tmp
                                            [error] => 0
                                            [size] => 148940
                                        )
                                    [1] => Array
                                        (
                                            [name] => 500beige-zoom.jpg
                                            [type] => image/jpeg
                                            [tmp_name] => C:\xampp\tmp\php6C84.tmp
                                            [error] => 0
                                            [size] => 211420
                                        )
                                )
                        )
                )
        )
)

現在取得中:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [type] => Ball
                    [barcode] => 
                    [length] => 
                    [wpi] => 
                    [dyeable] => 
                    [feltable] => 
                    [sold_weight] => 
                    [gauge] => 
                    [price] => 
                    [pack_qty] => 
                    [shipping_weight] => 
                    [cost_price] => 
                )

            [files] => Array
                (
                    [1] => Array
                        (
                            [0] => Array
                                (
                                    [name] => 500aqua.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => C:\xampp\tmp\php1534.tmp
                                    [error] => 0
                                    [size] => 133659
                                )

                        )

                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [type] => Ball
                    [barcode] => 
                    [length] => 
                    [wpi] => 
                    [dyeable] => 
                    [feltable] => 
                    [sold_weight] => 
                    [gauge] => 
                    [price] => 
                    [pack_qty] => 
                    [shipping_weight] => 
                    [cost_price] => 
                )

            [1] => Array
                (
                    [type] => Ball
                    [barcode] => 
                    [length] => 
                    [wpi] => 
                    [dyeable] => 
                    [feltable] => 
                    [sold_weight] => 
                    [gauge] => 
                    [price] => 
                    [pack_qty] => 
                    [shipping_weight] => 
                    [cost_price] => 
                )

            [files] => Array
                (
                    [1] => Array
                        (
                            [0] => Array
                                (
                                    [name] => 500beige.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => C:\xampp\tmp\php1535.tmp
                                    [error] => 0
                                    [size] => 148940
                                )

                            [1] => Array
                                (
                                    [name] => 500beige-zoom.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => C:\xampp\tmp\php1545.tmp
                                    [error] => 0
                                    [size] => 211420
                                )

                        )

                    [2] => Array
                        (
                            [0] => Array
                                (
                                    [name] => 500beige.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => C:\xampp\tmp\php1546.tmp
                                    [error] => 0
                                    [size] => 148940
                                )

                            [1] => Array
                                (
                                    [name] => 500beige-zoom.jpg
                                    [type] => image/jpeg
                                    [tmp_name] => C:\xampp\tmp\php1547.tmp
                                    [error] => 0
                                    [size] => 211420
                                )

                        )

                )

        )

)

私がこれまでに試したこと:

$group = array();
foreach($array1 as $key1 => $val1){
    $group[] = array_merge($val1, array("files" => $array2[$key1]));
}
4

1 に答える 1

1

私はまだそれをテストしていません (約 1 時間の時間がかかるかもしれません) が、これが役立つと思います:

foreach($array1 as $key => $val){
   foreach($val['option'] as $k_inner => $v_inner){
      $array1[$key]['option'][$k_inner]['files'] = $array2[$key][$k_inner];
   }
}

array2 が$group配列になります。テストしたらすぐに、これを編集します。

編集: アルゴリズムを修正しました。間違いは、$array1 と $array2 を混同し、正しい深さとキーを考慮していなかったという事実にありました。

于 2013-09-03T13:51:30.737 に答える