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

时间:2017-05-18 21:03:56

标签: php arrays multidimensional-array array-merge sub-array

对于这么简单的问题感到抱歉,但我是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
        )

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

$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'];也没有工作。

1 个答案:

答案 0 :(得分:1)

可悲的是,需要进行大量的迭代/准备才能使两个阵列准备好与array_merge_recursive()一起使用。我承认,我并不为我的方法中的卷积感到骄傲。所有这一切的主要因素是array_merge_recursive()只有"很好地发挥"使用非数字索引,所以我不得不用数组中的相对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);

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

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

有关array_merge_recursive()如何运作的简单示例,请选择demo。尝试使用任一阵列中的键。如果您从数字字符串中移除#那么多,array_merge_recursive()将假设它处理数字索引并将其缩小。我将id保存为字符串的技巧是在#之前添加,但可以通过在键值中添加任意一系列非数字字符来完成。