2016-06-05 6 views
0

여러 트랜잭션이 포함 된 배열이 있고 여러 전자 메일 주소가 동일한 트랜잭션이 있습니다.php - 배열의 값을 다차원 배열의 다른 배열로 복사

일부 트랜잭션의 값은 client입니다. 다른 사람들은 그렇지 않습니다.

항상 일치하는 배열의 client 키를 email_address 키와 함께 복사하여 client 키의 데이터를 항상 복사하려고합니다. 내 배열의

예 :

Array 
(
    [1] => Array 
     (
      [client] => John John 
      [email_address] => [email protected] 
     ) 

    [3] => Array 
     (
      [client] => Kevin Kevin 
      [email_address] => [email protected] 
     ) 

    [5] => Array 
     (
      [client] => 
      [email_address] => [email protected] 
     ) 

) 

내가 달성하고자하는 것은 (email_address 키를 기반으로) 같은 클라이언트이기 때문에 Array [5]Array [1]client 키에 같은 값을 가지고 있는지 확인하는 것입니다. 생성 된 배열의

예 :

나는 배열을 통해 갈 수있는 방법
Array 
(
    [1] => Array 
     (
      [client] => John John 
      [email_address] => [email protected] 
     ) 

    [3] => Array 
     (
      [client] => Kevin Kevin 
      [email_address] => [email protected] 
     ) 

    [5] => Array 
     (
      [client] => John John 
      [email_address] => [email protected] 
     ) 

) 

이 (email_address 키를 기반으로) 배열을 일치 항상 같은 client 키가 있는지 확인하기 위해?

+0

이 ... –

+0

@RivnatNasah 모든, 채워진'client' 키에 포함 된 어떤 데이터를 얼마나'. 그래서, Array [1]의'John John'은'Array [5]'의 John John입니다. – Lee

+0

그래서 당신의 질문은 무엇입니까? "X를하고 싶다"또는 "X를 쓰는 코드를 작성하십시오"는 질문이 아닙니다. – melpomene

답변

1

array_column, array_unique, array_flip, array_count_values, array_filterarray_intersect_key 기능 (이 용액은 또한 동일한 "EMAIL_ADDRESS"와 "클라이언트"항목의 여러 그룹을 처리하기에 적합하다)를 사용하여 용액 :

// supposing $arr is your initial array 

$ties = array_flip(array_unique(array_column($arr, "email_address", "client"))); 
$counts = array_filter(array_count_values(array_column($arr, "email_address")), function($v){ 
    return $v > 1; // getting number of entries with same 'email' attribute 
});  
$relations = array_intersect_key($ties, $counts); // contains pairs of relative email/client entries, like "[[email protected]] => John John" 

foreach ($arr as &$client) { 
    if (!$client['client'] && key_exists($client['email_address'], $relations)) { 
     $client['client'] = $relations[$client['email_address']]; 
    } 
} 

print_r($arr); 

출력 : 채우려 client`

Array 
(
    [1] => Array 
     (
      [client] => John John 
      [email_address] => [email protected] 
     ) 

    [3] => Array 
     (
      [client] => Kevin Kevin 
      [email_address] => [email protected] 
     ) 

    [5] => Array 
     (
      [client] => John John 
      [email_address] => [email protected] 
     ) 
) 
0

전자 메일 주소를 클라이언트와 관련시키는 중간 배열을 만들 수 있습니다. 이처럼 : 다음

$intermediateArray = []; 

foreach ($inputArray as $row) { 
    if (isset($row['client']) { 
    $intermediateArray[$row['email_address']] = $row['client'] 
    } 
} 

및 누락 된 클라이언트 세포를 입력 :

foreach ($inputArray as $row) { 
    if (!isset($row['client']) { 
    if (isset($intermediateArray[$row['email_address']])) { 
     $row['client'] = $intermediateArray[$row['email_address']]; 
    } 
    } 
} 

보다 효율적인 솔루션이 가능하지만,이 하나는 간단한 것 같다.

관련 문제