php - 将数组中的值复制到多维数组中的另一个数组中

php  - 将数组中的值复制到多维数组中的另一个数组中

问题描述:

I have an array that contains multiple transactions, with multiple transactions being from the same email address.

Some of these transactions have a value in the client key. Others don't.

I want to copy the filled client key in all matching arrays with the same email_address key, in order to always have data in the client key.

Example of my array:

Array
(
    [1] => Array
        (
            [client] => John John
            [email_address] => john@john.com
        )

    [3] => Array
        (
            [client] => Kevin Kevin
            [email_address] => kevin@kevin.com
        )

    [5] => Array
        (
            [client] => 
            [email_address] => john@john.com
        )

)

What I want to achieve is to make sure that Array [5] has the same value in the client key as Array [1] because it's the same client (based on email_address key).

Example of the resulted array:

Array
(
    [1] => Array
        (
            [client] => John John
            [email_address] => john@john.com
        )

    [3] => Array
        (
            [client] => Kevin Kevin
            [email_address] => kevin@kevin.com
        )

    [5] => Array
        (
            [client] => John John
            [email_address] => john@john.com
        )

)

How can I go through the array to make sure that matching arrays (based on email_address key) always have the same client key?

我有一个包含多个交易的数组,多个交易来自同一个电子邮件地址。 p> \ n

其中一些事务在 client code>键中有一个值。 其他人没有。 p>

我想在所有匹配的数组中使用相同的 email_address code>键复制已填充的 client code>键,按顺序 始终在 client code>键中包含数据。 p>

我的数组示例: p>

  Array 
(  
 [1] =>数组
(
 [客户端] =>约翰约翰
 [email_address] => john@john.com 
)
 
 [3] =>数组\  n(
 [client] => Kevin Kevin 
 [email_address] => kevin@kevin.com 
)
 
 [5] =>数组
(
 [客户端] =>  
 [email_address] => john@john.com 
)
 
)
  code>  pre> 
 
 

我想要实现的是确保 Array [5] code>在 client code>键中与 Array [1] code>具有相同的值,因为它是相同的客户端(基于 email_address code> key)。 p>

结果数组的示例: p>

  Array 
(
 [1] =>数组\  n(
  [client] =>  John John 
 [email_address] =>  john@john.com 
)
 
 [3] => 数组
(
 [客户端] =>凯文凯文
 [email_address] => kevin@kevin.com 
)
 
 [5] => 数组
(
 [客户端] =>约翰约翰
 [email_address] => john@john.com 
)
 
)
  code>  pre> 
 
 如何通过数组确保匹配的数组(基于 email_address  code>键)始终具有相同的 client  code>键? p> 
   DIV>

The solution using array_column, array_unique, array_flip, array_count_values, array_filter and array_intersect_key functions (This solution is also well-suited for processing multiple groups of "client" entries with same "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 "[john@john.com] => John John"

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

print_r($arr);

The output:

Array
(
    [1] => Array
        (
            [client] => John John
            [email_address] => john@john.com
        )

    [3] => Array
        (
            [client] => Kevin Kevin
            [email_address] => kevin@kevin.com
        )

    [5] => Array
        (
            [client] => John John
            [email_address] => john@john.com
        )
)

You can make an intermediate array that relates email addresses to client. Like this:

$intermediateArray = [];

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

and then fill in the missing client cells:

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

More efficient solutions are possible, but this one seems the simplest.