I have 3 database queries (For query results overview see below!).
How can I merge the elements of this three arrays (with more than 200.000 products in DB) together without duplicate array keys and values as the following example?
Desired result:
Array
(
[0] => Array
(
[id] => 42710
[imageName] => somthing
[image] => https://www.somthing.com/image.jpg
[loyality] => 122
[p_num] => 8
[cpc] => 2
)
[...]
)
I have tried to use array functions array_merge
, array_merge_recursive
, array_replace
, array_replace_recursive
and array_unique
. But I could not merge clearly all arrays together.
Result of query 1 (>200000 total):
Array
(
[0] => Array
(
[id] => 42710
[imageName] => somthing
[image] => https://www.somthing.com/image.jpg
)
[...]
)
Result of query 2 (1450 total):
Array
(
[0] => Array
(
[id] => 42710
[loyality] => 122
)
[...]
)
Result of query 3 (1820 total):
Array
(
[0] => Array
(
[id] => 42710
[p_num] => 8
[cpc] => 2
)
[...]
)
Note: Please don't make a suggestion based on "SQL JOIN clause".
I am using PHP5.6, Symfony2, Doctrine2 and MySQL 5.5.52.
I could find a better solution for large arrays.
First fetching data through a custom repository:
$this->productAttributes = $this->manager->getRepository('ProductBundle:Rating')->findAllAttributeOfProducts();
$this->productOnlineDate = $this->manager->getRepository('ProductBundle:Rating')->findOnlineDate();
$this->numberOfClicks = $this->manager->getRepository('ProductBundle:Rating')->findNumberOfClicks();
Second find productID in all arrays by means of array_search
and array_column
:
$findNumberOfClicks = array_search($this->arrayKey['id'], array_column($this->numberOfClicks, 'id'));
$findOnlineDate = array_search($this->arrayKey['id'], array_column($this->productOnlineDate, 'id'));
Third merge arrays all together:
$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];
}
}