合并两个多维数组

时间:2017-12-22 22:24:11

标签: php arrays

我有一个MySQL表types,我存储了产品类型。我取出它们并得到这个数组:

 [0]=>
    ['unique_codename']=>'cars'
    ['category']=>'vehicle'
    …some other stuf…
 [1]=>
    ['unique_codename']=>'fruit'
    ['category']=>'food'
    …some other stuf…
 [2]=>
    ['unique_codename']=>'vegetables'
    ['category']=>'food'
    …some other stuf…
…

接下来,我有一个包含特定products的表格。我可以把它们全部取出来得到:

 [0]=>
    ['codename']=>'fruit'
    ['name']=>'banana'
    …some other stuf…
 [1]=>
    ['codename']=>'fruit'
    ['name']=>'apple'
    …some other stuf…
 [2]=>
    ['codename']=>'vegetables'
    ['name']=>'cauliflower'
    …some other stuf…
 [3]=>
    ['codename']=>'cars'
    ['name']=>'audi'
    …some other stuf…
 [4]=>
    ['codename']=>'cars'
    ['name']=>'volvo'
    …some other stuf…
…

我想构建一个包含所有信息的单个数组,如下所示:

 [0]=>
    ['unique_codename']=>'cars'
    ['sorts']=>
    [0]=>
        ['name'] = 'audi'
        …
    [1]=>
        ['name'] = 'volvo'
        …
    ['category']=>'vehicle'
    …

 [1]=>
    ['unique_codename']=>'fruit'
    ['sorts']=>
    [0]=>
        ['name'] = 'banana'
        …
    [1]=>
        ['name'] = 'apple'
        …
    ['category']=>'food'
    …

 [2]=>
    ['unique_codename']=>'vegetables'
    ['sorts']=>
    [0]=>
        ['name'] = 'cauliflower'
        …
    ['category']=>'food'
    …
…

我正在考虑首先获取两个数组。接下来,我可以在适当的位置推送第一个数组,但是我无法弄清楚使用array_push时这将如何工作。谁可以帮助我?或者有更优雅的解决方案来实现这一目标吗?

2 个答案:

答案 0 :(得分:1)

这是一种技术,它将构建一个列表,该列表保留所有类型和产品的未指定属性,并按类型分组

// Build a list of known types
$typeIndex = [];
foreach ($types AS $id=>$type) {
    $typeIndex[] = $type['unique_codename'];
    $merged[$id] = $type;
}

foreach ($products AS $product) {
    $codeIndex = array_search($product['codename'], $typeIndex);
    if ($codeIndex === FALSE) continue;

    // Create sorts array to hold product if necessary
    if (!isset($merged[$codeIndex]['sorts'])) {
        $merged[$codeIndex]['sorts'] = [];
    }

    // Insert product
    $merged[$codeIndex]['sorts'][] = $product;
}

答案 1 :(得分:0)

您要做的是按特定列对多维数组进行分组,然后将其合并。

通过创建一个新的关联数组,使用类别作为键,可以使用array_reduce进行分组。

$groupedProducts = array_reduce($products, function($carry, $product){
    if (!array_key_exists($product['codename'], $carry)) {
        $carry[$product['codename']] = [];
    }
    $carry[$product['codename']][] = $product;
    return $carry;
}, []);

这将创建以下数据结构:

$groupedProducts = [
    'fruit' => [
        [
            'codename' => 'fruit',
            'name' => 'banana',
            ... some other stuff ...
        ],
        [
            'codename' => 'fruit',
            'name' => 'apple',
            ... some other stuff ...
        ]
    ],
    'vegetables' => [
        [
            'codename' => 'vegetables',
            'name' => 'cauliflower',
            ... some other stuff ...
        ]
    ],
    'cars' => [
        [
            'codename' => 'cars',
            'name' => 'audi',
            ... some other stuff ...
        ],
        [
            'codename' => 'cars',
            'name' => 'volvo',
            ... some other stuff ...
        ]
    ],
]

如您所见,所有产品都按代号分组。如果你不想在内部数组中使用代号模式密钥,可以在unset匿名函数中array_reduce,或者在其中使用array_intersect_key来选择要保留的特定密钥。

如果您想按类别对最终数组进行分组,则可以稍微调整一下代码。

接下来是合并。在这里,您可以在初始数组上使用array_map向其中添加排序:

$finalArray = array_map(function($type) use ($groupedProducts) {
    if (array_key_exists($type['unique_codename'], $groupedProducts)) {
       $type['sorts'] = $groupedProducts[$type['unique_codename']];
    }
    return $type;
}, $types);

此代码将创建您想要构建的最终数组。

相关问题