2017-01-13 67 views
2

我有3個數據庫查詢(查詢結果概述見下文!)。如何合併具有相同ID的三個數組的元素?

我該如何將這三個數組的元素合併在一起,並且沒有重複的數組鍵和值,如下例所示:

期望的結果:

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 
      [loyality] => 122 
     ) 

    [...] 

) 

查詢3(1820個)的結果::

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

    [...] 
) 

查詢2(1450個)的結果

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

    [...] 

) 

注意:請不要提出建議ba sed on 「SQL JOIN子句」

我使用PHP5.6,Symfony2,Doctrine2和MySQL 5.5.52。

+0

可能這個帖子中已經有人問這個問題了嗎? [http://stackoverflow.com/questions/13469803/php-merging-two-array-into-one-array-also-remove-duplicates](http://stackoverflow.com/questions/13469803/php-merging -two-array-into-one-array-also-remove-duplicates) –

+0

從數據庫中抽取200k行只是爲了過濾99%的數據? – shudder

+0

'[id]'是否代表每個陣列完全一樣的東西?我想知道,因爲下面你提到有兩個數組,每個數組都有一個年齡但有不同的值? –

回答

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(); 

二發現的productID在所有陣列由array_searcharray_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]; 
       } 

      } 
0

像這樣遍歷每個數組,保存結果數組$o中的元素。

$o = []; 
array_walk($array1, function($v) use (&$o) { 
    foreach($v as $key => $value) { 
     $o[$v['id']][$key] = $value; 
    } 
}); 
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); 
+0

不幸的是它錯了。 添加一個具有不同「id」和不同年齡值的數組並查看結果。 –

+0

已更新,完全符合您的需求。 –

相關問題