数组合并两个具有不同项目数的数组,其中有匹配的productid

时间:2017-05-18 07:32:09

标签: php arrays merge

我正在尝试合并我拥有的两个数组,以便最终得到一个数组。这是我的第一个数组的例子:

Array
(
    [0] => Array
        (
            [name] => Name1
            [price] => 237.00
            [colour] => Blue
            [image] => http:.............
            [url] => http:.............
            [productid] => 7740792
        )

    [1] => Array
        (
            [name] => Name2
            [price] => 87.00
            [colour] => Mauve
            [image] => http://.......
            [url] => http://.......
            [productid] => 7638070
        )

以下是我的第二个数组的示例:

Array
(
    [productId] => 7740792
    [productCode] => 1019534
    [variants] => Array
        (
            [0] => Array
                (
                    [VariantID] => 7740915
                    [isInStock] => 1
                )

            [1] => Array
                (
                    [VariantID] => 7740906
                    [isInStock] => 1
                )

            [2] => Array
                (
                    [VariantID] => 7740916
                    [isInStock] => 1
                )

            [3] => Array
                (
                    [VariantID] => 7740923
                    [isInStock] => 1
                )

            [4] => Array
                (
                    [VariantID] => 7740934
                    [isInStock] => 1
                )

            [5] => Array
                (
                    [VariantID] => 7740922
                    [isInStock] => 1
                )

            [6] => Array
                (
                    [VariantID] => 7740933
                    [isInStock] => 1
                )

            [7] => Array
                (
                    [VariantID] => 7740942
                    [isInStock] => 1
                )

        )

)
Array
(
    [productId] => 7638070
    [productCode] => 1004120
    [variants] => Array
        (
            [0] => Array
                (
                    [VariantID] => 7638099
                    [isInStock] => 1
                )

            [1] => Array
                (
                    [VariantID] => 7638121
                    [isInStock] => 1
                )

            [2] => Array
                (
                    [VariantID] => 7638117
                    [isInStock] => 1
                )

            [3] => Array
                (
                    [VariantID] => 7638126
                    [isInStock] => 1
                )

            [4] => Array
                (
                    [VariantID] => 7638131
                    [isInStock] => 1
                )

            [5] => Array
                (
                    [VariantID] => 7638092
                    [isInStock] => 1
                )

            [6] => Array
                (
                    [VariantID] => 7638135
                    [isInStock] => 1
                )

            [7] => Array
                (
                    [VariantID] => 7638091
                    [isInStock] => 1
                )

        )

)

我想要做的是最终得到一个如下所示的数组:

Array
(
    [0] => Array
        (
            [name] => Name1
            [price] => 237.00
            [colour] => Blue
            [image] => http:.............
            [url] => http:.............
            [productid] => 7740792

            [productId] => 7740792 
            [productCode] => 1004120
                  [variants] => Array
        (
            [0] => Array
                (
                    [VariantID] => 7638099
                    [isInStock] => 1
                )

            [1] => Array
                (
                    [VariantID] => 7638121
                    [isInStock] => 1
                )

            [2] => Array
                (
                    [VariantID] => 7638117
                    [isInStock] => 1
                )

            [3] => Array
                (
                    [VariantID] => 7638126
                    [isInStock] => 1
                )

            [4] => Array
                (
                    [VariantID] => 7638131
                    [isInStock] => 1
                )

            [5] => Array
                (
                    [VariantID] => 7638092
                    [isInStock] => 1
                )

            [6] => Array
                (
                    [VariantID] => 7638135
                    [isInStock] => 1
                )

            [7] => Array
                (
                    [VariantID] => 7638091
                    [isInStock] => 1
                )

我希望这是有道理的。我已经尝试过阵列组合,但由于阵列中有不同数量的项目,因此无法工作。我正在学习PHP,很抱歉,如果这已经得到了解答,但我还没有能够将答案中的其他代码应用到我的案例中。

2 个答案:

答案 0 :(得分:1)

如果您可以设法将产品ID作为变量阵列的关键,那就快速而简单:

for ($j = 0; $j <= count($names); $j++) {
    if (array_key_exists($names[$j]['productId'],$variants) {
      $names[$j][variants] = $variants[$names[$j]['productId']]['variants'];
    }
}

否则,如果您保留原样,则必须进行搜索,这很糟糕:

for ($j = 0; $j <= count($names); $j++) {
   for ($i = 0; $i <= count($variants); $i++) {
      if ($names[$j]['prodcutId'] == $variants[i]['productId']) {
        $names[$j]['variants'] = $variants[$i]['variants'];
        break;
      }        
   }
}

答案 1 :(得分:0)

试试这个:

foreach($second_array as $k => $v){
    $key = array_search($v['productId'], array_column($first_array, 'productid'));
    if($key !==false){
         $newArray[$k] = array_merge($second_array[$k], $first_array[$key]);
    }
}
var_dump($newArray);