如何添加两个多维数组的合并子数组

How to add merge subarrays of two multi-dimensional arrays

很抱歉提出这么简单的问题,但我是 PHP 的新手。 我正在尝试添加此数组中的 isInStock 键和值:

$stock数组:

Array
(
    [0] => Array
        (
            [name] => name of item 1
            [price] => 45.00
            [colour] => Neon yellow
            [image] => http://images1
            [url] => http://url1
            [productid] => 7985894
            [variants] => Array
                (
                    [0] => Array
                        (
                            [variantId] => 7986029
                            [isInStock] => 1
                        )
                    [1] => Array
                        (
                            [variantId] => 7986070
                            [isInStock] => 1
                        )
                    [2] => Array
                        (
                            [variantId] => 7985916
                            [isInStock] => 1
                        )
                    [3] => Array
                        (
                            [variantId] => 7985929
                            [isInStock] => 1
                        )

                    [4] => Array
                        (
                            [variantId] => 7985918
                            [isInStock] => 1
                        )
                    [5] => Array
                        (
                            [variantId] => 7985935
                            [isInStock] => 1
                        )
                    [6] => Array
                        (
                            [variantId] => 7985945
                            [isInStock] => 1
                        )
                    [7] => Array
                        (
                            [variantId] => 7985994
                            [isInStock] => 1
                        )
                )
            [productId] => 7985894
        )
    [1] => Array
        (
            [name] => name of item 2
            [price] => 45.00
            [colour] => Multi
            [image] => http://images
            [url] => http://url
            [productid] => 8040851
            [variants] => Array
                (
                    [0] => Array
                        (
                            [variantId] => 8040898
                            [isInStock] => 1
                        )
                    [1] => Array
                        (
                            [variantId] => 8041115
                            [isInStock] => 1
                        )
                    [2] => Array
                        (
                            [variantId] => 8040904
                            [isInStock] => 1
                        )
                    [3] => Array
                        (
                            [variantId] => 8041132
                            [isInStock] => 1
                        )
                    [4] => Array
                        (
                            [variantId] => 8041015
                            [isInStock] => 1
                        )
                    [5] => Array
                        (
                            [variantId] => 8040942
                            [isInStock] => 1
                        )
                    [6] => Array
                        (
                            [variantId] => 8040954
                            [isInStock] => 1
                        )
                    [7] => Array
                        (
                            [variantId] => 8040990
                            [isInStock] => 1
                        )
                )
            [productId] => 8040851
        )

并根据 variantId 值将它们放在每个 size 下的数组中,如果它们都匹配。

$data数组:

Array
(
    [0] => Array
        (
            [name] => name 1 
            [price] => 45.00
            [colour] => Neon yellow
            [image] => http://url
            [url] => http://url1
            [productid] => 7985894
            [variants] => Array
                (
                    [0] => Array
                        (
                            [variantId] => 7986029
                            [size] => US 0
                        )
                    [1] => Array
                        (
                            [variantId] => 7986070
                            [size] => US 2
                        )
                    [2] => Array
                        (
                            [variantId] => 7985916
                            [size] => US 4
                        )
                    [3] => Array
                        (
                            [variantId] => 7985929
                            [size] => US 6
                        )
                    [4] => Array
                        (
                            [variantId] => 7985918
                            [size] => US 8
                        )
                    [5] => Array
                        (
                            [variantId] => 7985935
                            [size] => US 10
                        )
                    [6] => Array
                        (
                            [variantId] => 7985945
                            [size] => US 12
                        )
                    [7] => Array
                        (
                            [variantId] => 7985994
                            [size] => US 14
                        )
                )
        )
    [1] => Array
        (
            [name] => name 1
            [price] => 45.00
            [colour] => Multi
            [image] => http://url
            [url] => http://url
            [productid] => 8040851
            [variants] => Array
                (
                    [0] => Array
                        (
                            [variantId] => 8040898
                            [size] => US 0
                        )
                    [1] => Array
                        (
                            [variantId] => 8041115
                            [size] => US 2
                        )
                    [2] => Array
                        (
                            [variantId] => 8040904
                            [size] => US 4
                        )
                    [3] => Array
                        (
                            [variantId] => 8041132
                            [size] => US 6
                        )
                    [4] => Array
                        (
                            [variantId] => 8041015
                            [size] => US 8
                        )
                    [5] => Array
                        (
                            [variantId] => 8040942
                            [size] => US 10
                        )
                    [6] => Array
                        (
                            [variantId] => 8040954
                            [size] => US 12
                        )
                    [7] => Array
                        (
                            [variantId] => 8040990
                            [size] => US 14
                        )
                )
        )

我已经尝试解决这个问题一段时间了,但一直卡壳。我可以分配单个值,但不能让它完成整个数组。任何帮助,将不胜感激。

我希望我的结果数组是这样的:

Array
(
    [0] => Array
        (
            [name] => name 1 
            [price] => 45.00
            [colour] => Neon yellow
            [image] => http://url
            [url] => http://url1
            [productid] => 7985894
            [variants] => Array
                (
                    [0] => Array
                        (
                            [variantId] => 7986029
                            [size] => US 0
                            [isInStock] => 1
                        )    
                    [1] => Array
                        (
                            [variantId] => 7986070
                            [size] => US 2
                            [isInStock] => 1
                        )    
                    [2] => Array
                        (
                            [variantId] => 7985916
                            [size] => US 4
                            [isInStock] => 1
                        )    
                    [3] => Array
                        (
                            [variantId] => 7985929
                            [size] => US 6
                            [isInStock] => 1
                        )    
                    [4] => Array
                        (
                            [variantId] => 7985918
                            [size] => US 8
                            [isInStock] => 1
                        )    
                    [5] => Array
                        (
                            [variantId] => 7985935
                            [size] => US 10
                            [isInStock] => 1
                        )    
                    [6] => Array
                        (
                            [variantId] => 7985945
                            [size] => US 12
                            [isInStock] => 1
                        )    
                    [7] => Array
                        (
                            [variantId] => 7985994
                            [size] => US 14
                            [isInStock] => 1
                        )    
                )    
        )    
    [1] => Array
        (
            [name] => name 1
            [price] => 45.00
            [colour] => Multi
            [image] => http://url
            [url] => http://url
            [productid] => 8040851
            [variants] => Array
                (
                    [0] => Array
                        (
                            [variantId] => 8040898
                            [size] => US 0
                            [isInStock] => 1
                        )    
                    [1] => Array
                        (
                            [variantId] => 8041115
                            [size] => US 2
                            [isInStock] => 1
                        )    
                    [2] => Array
                        (
                            [variantId] => 8040904
                            [size] => US 4
                            [isInStock] => 1
                        )    
                    [3] => Array
                        (
                            [variantId] => 8041132
                            [size] => US 6
                            [isInStock] => 1
                        )    
                    [4] => Array
                        (
                            [variantId] => 8041015
                            [size] => US 8
                            [isInStock] => 1
                        )    
                    [5] => Array
                        (
                            [variantId] => 8040942
                            [size] => US 10
                            [isInStock] => 1
                        )    
                    [6] => Array
                        (
                            [variantId] => 8040954
                            [size] => US 12
                            [isInStock] => 1
                        )    
                    [7] => Array
                        (
                            [variantId] => 8040990
                            [size] => US 14
                            [isInStock] => 1
                        )    
                )    
        )

到目前为止,我已经尝试了 array_merge,它没有将值放在正确的位置。 我试过这个 $data['isInstock'] = $stock[0]['variants'][0]['variantId']; 也行不通。

遗憾的是,需要大量 iterating/preparation 才能让您的两个数组准备好与 array_merge_recursive() 一起使用。我承认,我对我的方法中的卷积并不感到自豪。所有这一切的主要因素是 array_merge_recursive() 只有 "plays nicely" 具有非数字索引,所以我不得不用数组中的相对 id 值替换你的数字索引键。我会尽力解释我的步骤,但同样,它并不漂亮...(Demo)

第 1 步:准备 $stock 数组:

foreach($stock as $subarray){
    $new_stock["#{$subarray['productid']}"]=$subarray;  // replace outer key
    $new_variants=[];                                   // declare a fresh array
    foreach($subarray['variants'] as $varsub){
        $new_variants["#{$varsub['variantId']}"]['isInStock']=$varsub['isInStock'];  // one element only
        // omitting variantId element this time as the next array will offer it.
    }
    $new_stock["#{$subarray['productid']}"]['variants']=$new_variants;
}

步骤 #2:准备 $data 数组并合并:

foreach($data as $subarray){
    $new_data["#{$subarray['productid']}"]=$subarray;  // replace outer key
    $new_variants=[];                                  // declare a fresh array
    foreach($subarray['variants'] as $varsub){
        $new_variants["#{$varsub['variantId']}"]=$varsub; // both elements from variants
    }
    $new_data["#{$subarray['productid']}"]['variants']=array_values(array_merge_recursive($new_variants,$new_stock["#{$subarray['productid']}"]['variants']));
    // new variants subarray has been merged, re-indexed, and written to $new_data
}

步骤#3:重新索引外部数组键,并显示:

$result=array_values($new_data);    
var_export($result);

数组准备工作的大部分是为外部和内部数组(在 $stock$data 中)生成唯一的 ID。这允许 array_merge 隔离相关的 productid 并递归合并 variant 元素。

如果这两个数组是从数据库生成的,那么我的强烈建议是利用可用的数据库功能来合并这些数据,而不是 php。

有关 array_merge_recursive() 如何工作的简单示例,这里有一个小 demo。尝试使用任一数组中的键。如果您几乎从数字字符串中删除 #array_merge_recursive() 将假定它正在处理数字索引并把事情搞砸。我将您的 id 保留为字符串的技术是在 # 之前添加,但它可以通过将一系列非数字字符中的任何一个添加到键值来完成。