2017-01-13 3 views
2

데이터베이스 쿼리가 3 개 있습니다 (쿼리 결과 개요는 아래 참조).어떻게 3 개의 배열 요소를 동일한 ID로 병합 할 수 있습니까?

은 (예 : DB에 200.000 개 이상의 제품이있는)이 세 가지 배열의 요소을 다음 예제와 같이 중복 된 배열 키와 값없이 병합 할 수 있습니까?

원하는 결과 : I가 배열 함수 array_merge 사용하려고 한

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

    [...]  
) 

, array_merge_recursive,array_replace, array_replace_recursivearray_unique. 그러나 모든 배열을 명확하게 병합 할 수는 없습니다. 질의 1


결과 (> 200,000 총) : 질의 3 (1,820 전체)

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

    [...] 

) 

결과 : 질의 2 (1450 계)의

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

    [...] 
) 

결과

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

    [...] 

) 

참고 : 제안 사항을 남기지 마십시오. "SQL JOIN 절"에 대한 sed.

PHP5.6, Symfony2, Doctrine2 및 MySQL 5.5.52를 사용하고 있습니다.

+0

5 월 수 있습니다.이 게시물에서 이미 물어볼 수 있습니까? [http://stackoverflow.com/questions/13469803/php-merging-two-array-into-one-array-also-remove-duplicates](http://stackoverflow.com/questions/13469803/php-merging 두 개의 배열을 하나의 배열에 또한 제거 - 중복) –

+0

당신은 그들의 99 %를 필터링하는 데이터베이스에서 200k 행을 당겨? – shudder

+0

'[id]'는 각 배열에 대해 정확히 같은 것을 나타 냅니까?나는 당신이 나이가 있지만 각기 다른 값을 가지고 각각 2 개의 배열을 가지고 있다고 언급하기 때문에 궁금합니다. –

답변

0

내가 큰 배열을위한 더 나은 해결책을 찾을 수 있습니다.

먼저 사용자 정의 저장소를 통해 데이터를 가져 오는 : 모두 함께

$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]; 
       } 

      } 

$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에 의해 모든 배열에 제품 ID를 찾을 수

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'와 age 값이 다른 배열을 하나 더 추가하고 결과를 봅니다. –

+0

이 원하는대로 정확하게 업데이트되었습니다. –