doumao1887 2016-06-05 08:37
浏览 149
已采纳

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?

  • 写回答

2条回答 默认 最新

  • dongti7838 2016-06-05 09:47
    关注

    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
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误