我有3个数据库查询(查询结果概述见下文!)。
如何合并这三个数组的元素(数据库中的产品数量超过200.000),而不需要重复的数组键和值,如下例所示?
期望的结果:
Array
(
[0] => Array
(
[id] => 42710
[imageName] => somthing
[image] => https://www.somthing.com/image.jpg
[loyality] => 122
[p_num] => 8
[cpc] => 2
)
[...]
)
我尝试使用数组函数 array_merge
, array_merge_recursive
, array_replace
,< strong> array_replace_recursive
和 array_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
)
[...]
)
注意:请不要根据&#34; SQL JOIN条款&#34; 提出建议。
我使用的是PHP5.6,Symfony2,Doctrine2和MySQL 5.5.52。
答案 0 :(得分: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);
答案 1 :(得分:0)
以这种方式遍历每个数组,保存结果数组$o
中的元素。
$o = [];
array_walk($array1, function($v) use (&$o) {
foreach($v as $key => $value) {
$o[$v['id']][$key] = $value;
}
});
答案 2 :(得分:0)
我可以为大型阵列找到更好的解决方案。
首先通过自定义存储库获取数据:
$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_search
和array_column
$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];
}
}