如何合并具有相同 ID 的三个数组的元素?
How can I merge the elements of three arrays with same ID's?
我有 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
、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
)
[...]
)
注意:请不要根据"SQL JOIN clause".
提出建议
我正在使用 PHP5.6、Symfony2、Doctrine2 和 MySQL 5.5.52.
这样遍历每个数组,将元素保存在结果数组$o
.
$o = [];
array_walk($array1, function($v) use (&$o) {
foreach($v as $key => $value) {
$o[$v['id']][$key] = $value;
}
});
您可以使用 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);
我可以为大型数组找到更好的解决方案。
首先通过自定义存储库获取数据:
$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
在所有数组中查找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];
}
}
我有 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
、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
)
[...]
)
注意:请不要根据"SQL JOIN clause".
提出建议我正在使用 PHP5.6、Symfony2、Doctrine2 和 MySQL 5.5.52.
这样遍历每个数组,将元素保存在结果数组$o
.
$o = [];
array_walk($array1, function($v) use (&$o) {
foreach($v as $key => $value) {
$o[$v['id']][$key] = $value;
}
});
您可以使用 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);
我可以为大型数组找到更好的解决方案。
首先通过自定义存储库获取数据:
$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
在所有数组中查找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];
}
}