php 中子级的映射数组

Map Array in child in php

我在 php 中有 2 个数组,如下所示:

array1 = [product_id = [data], product_id = [data], ..]

[
    101 => [
        "sku" => "AB01"
    ],
    201 => [
        "sky" => "AB02"
    ],
    ...
]

array2 = attribute of product with product_id

    [
    0 => [
        "product_id" => 101,
        "name" => "pro 1"
    ],
    1 => [
        "product_id" => 101,
        "size" => "S"
    ],
    2 => [
        "product_id" => 201,
        "name" => "pro 2"
    ],
    3 => [
        "product_id" => 201,
        "size" => "S"
    ],
    ...
]

我想要的是根据 product_id 在 array2 中将数据作为子数组推送到 array1 中

    [
    101 => [
        "sku" => "AB01",
        "attributes" => [
            0 => [
                "product_id" => 101,
                "name" => "pro 1"
            ],
            1 => [
                "product_id" => 101,
                "size" => "S"
            ]
        ]
    ],
    201 => [
        "sky" => "AB02",
        "attributes" => [
            0 => [
                "product_id" => 201,
                "name" => "pro 2"
            ],
            1 => [
                "product_id" => 201,
                "size" => "S"
            ]
        ]
    ],
    ...
]

array1 的数组长度约为 1000,array2 的数组长度 >5000。 foreach 循环花费太多时间。有什么快速实现的方法吗?

我能想到的最快的实现方式是在第二个数组上使用 foreach 循环,因为第一个数组由 product_id 索引,很容易直接在每个数组上插入新数据循环...

foreach ( $array2 as $item )    {
    $array1[$item['product_id']]['attributes'][] = $item;
}

希望这个回答对你有用

第一个数组

$arr = [
    101 => [
        "sku" => "AB01"
    ],
    201 => [
        "sky" => "AB02"
    ],
];

第二个数组

$arr2 = [
    0 => [
        "product_id" => 101,
        "name" => "pro 1"
    ],
    1 => [
        "product_id" => 101,
        "size" => "S"
    ],
    2 => [
        "product_id" => 201,
        "name" => "pro 2"
    ],
    3 => [
        "product_id" => 201,
        "size" => "S"
    ],
];

最后这就是我尝试做的事情

$newArr = [];

forEach($arr as $product_id => $product_value){
    $attrArr = [];
    forEach($arr2 as $key => $value){
        if($product_id == $value['product_id']){

            $attrArr[] = $value;

            $newArr[$product_id] = [
                array_keys($product_value)[0] => array_values($product_value)[0],
            ];
        }

    }
    $newArr[$product_id]['attributes'] = $attrArr;
}

echo '<pre>';
print_r($newArr);
echo '</pre>';

也许对你有帮助

$array1 = [
            101 => [
                "sku" => "AB01"
            ],
            201 => [
                "sky" => "AB02"
            ]
        ];

$array2 = [
            0 => [
                "product_id" => 101,
                "name" => "pro 1"
            ],
            1 => [
                "product_id" => 101,
                "size" => "S"
            ],
            2 => [
                "product_id" => 201,
                "name" => "pro 2"
            ],
            3 => [
                "product_id" => 201,
                "size" => "S"
            ]
        ] ;

foreach ($array2 as $result){
    if(array_key_exists($result['product_id'], $array1)) {
        $array1[$result['product_id']]['attributes'][] = $result;
    }
}

print_r($array1);

你的结果看起来像这样

Array
(
    [101] => Array
        (
            [sku] => AB01
            [attributes] => Array
                (
                    [0] => Array
                        (
                            [product_id] => 101
                            [name] => pro 1
                        )

                    [1] => Array
                        (
                            [product_id] => 101
                            [size] => S
                        )

                )

        )

    [201] => Array
        (
            [sky] => AB02
            [attributes] => Array
                (
                    [0] => Array
                        (
                            [product_id] => 201
                            [name] => pro 2
                        )

                    [1] => Array
                        (
                            [product_id] => 201
                            [size] => S
                        )

                )

        )

)
$arr1=[
    101 => [
        "sku" => "AB01"
    ],
    201 => [
        "sky" => "AB02"
    ]
];
$arr2=[
    0 => [
        "product_id" => 101,
        "name" => "pro 1"
    ],
    1 => [
        "product_id" => 101,
        "size" => "S"
    ],
    2 => [
        "product_id" => 201,
        "name" => "pro 2"
    ],
    3 => [
        "product_id" => 201,
        "size" => "S"
    ]
];

foreach ($arr2 as $arr_val ) {
    $arr1[$arr_val['product_id']]['attributes'][] = $arr_val;
}

echo "<pre>"; print_r($arr1);