【问题标题】:How can I merge the elements of three arrays with same ID's?如何合并具有相同 ID 的三个数组的元素?
【发布时间】:2017-05-29 01:23:23
【问题描述】:

我有 3 个数据库查询(查询结果概览见下文!)。

如何合并这三个数组的元素(DB 中有超过 200.000 个产品),而不会出现重复的数组键和值,如下例所示?

想要的结果:

Array
(
    [0] => Array
        (
            [id] => 42710
            [imageName] => somthing
            [image] => https://www.somthing.com/image.jpg
            [loyality] => 122                
            [p_num] => 8
            [cpc] => 2
        )

    [...]    
)

我尝试过使用数组函数array_mergearray_merge_recursivearray_replacearray_replace_recursivearray_unique。但我无法将所有数组清晰地合并在一起。


查询 1 的结果(>200000 总):

Array
(
    [0] => Array
        (
            [id] => 42710
            [imageName] => somthing
            [image] => https://www.somthing.com/image.jpg
        )

     [...]
)

查询 2 的结果(共 1450 个):

Array
(
    [0] => Array
        (
            [id] => 42710
            [loyality] => 122
        )

    [...]

)

查询 3 的结果(共 1820 个):

Array
(
    [0] => Array
        (
            [id] => 42710
            [p_num] => 8
            [cpc] => 2
        )

    [...]

)

注意:请不要根据“SQL JOIN 子句”提出建议

我正在使用 PHP5.6、Symfony2、Doctrine2 和 MySQL 5.5.52。

【问题讨论】:

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


【解决方案1】:

你可以使用array_merge函数来合并结果,看下面的例子来澄清一下:

<?php

$resultSet1=[['id' => '32','name' => 'john'], ['id' => '15','name' => 'amin']];
$resultSet2=[['id' => '32','lastname' => 'doe'],['id' => '15','lastname' => 'alizade']];
$resultSet3=[['id' => '32','age' => '28'], ['id' => '15','age' => '25']];

$resultSet = array_merge($resultSet1, $resultSet2, $resultSet3);

$result = [];
foreach ($resultSet as $record) {   
    $key = $record['id'];
    if (array_key_exists($key, $result)) {
        $result[$key] = array_merge($result[$key], $record);
    }else{
        $result[$key] = $record;
    }
}

var_dump($result);

【讨论】:

  • 不幸的是它错了。再添加一个具有不同 'id' 和不同年龄值的数组并查看结果。
【解决方案2】:

这样遍历每个数组,将元素保存在结果数组$o中。

$o = [];
array_walk($array1, function($v) use (&$o) {
    foreach($v as $key => $value) {
        $o[$v['id']][$key] = $value;
    }
});

【讨论】:

    【解决方案3】:

    我可以为大型数组找到更好的解决方案。

    首先通过自定义存储库获取数据:

    $this->productAttributes = $this->manager->getRepository('ProductBundle:Rating')->findAllAttributeOfProducts();
    $this->productOnlineDate = $this->manager->getRepository('ProductBundle:Rating')->findOnlineDate();
    $this->numberOfClicks    = $this->manager->getRepository('ProductBundle:Rating')->findNumberOfClicks();
    

    第二次通过array_searcharray_column在所有数组中查找productID:

    $findNumberOfClicks = array_search($this->arrayKey['id'], array_column($this->numberOfClicks, 'id'));
    $findOnlineDate     = array_search($this->arrayKey['id'], array_column($this->productOnlineDate, 'id'));
    

    第三次合并数组:

    $this->productAttributes = $this->manager->getRepository('ProductBundle:Rating')->findAllAttributeOfProducts();
    $this->productOnlineDate = $this->manager->getRepository('ProductBundle:Rating')->findOnlineDate();
    $this->numberOfClicks    = $this->manager->getRepository('ProductBundle:Rating')->findNumberOfClicks();
    
    $this->arrayMergedResult = [];
    
                foreach ($this->productAttributes as $this->product => $this->arrayKey) {
    
                    // find product ID in all arrays
                    $findNumberOfClicks = array_search($this->arrayKey['id'], array_column($this->numberOfClicks, 'id'));
                    $findOnlineDate     = array_search($this->arrayKey['id'], array_column($this->productOnlineDate, 'id'));
    
                    // merge arrays
                    switch (true) {
                        case ($findOnlineDate === false && $findNumberOfClicks === false):
                            $this->arrayMergedResult = $this->arrayKey;
                            break;
                        case ($findOnlineDate === false):
                            $this->arrayMergedResult = $this->arrayKey + $this->numberOfClicks[$findNumberOfClicks];
                            break;
                        case ($findNumberOfClicks === false):
                            $this->arrayMergedResult = $this->arrayKey + $this->productOnlineDate[$findOnlineDate];
                            break;
                        default:
                            $this->arrayMergedResult = $this->arrayKey + $this->numberOfClicks[$findNumberOfClicks] + $this->productOnlineDate[$findOnlineDate];
                    }
    
                }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-08
      • 2021-12-09
      • 1970-01-01
      • 2014-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多