dpmwy80068 2019-02-08 05:39
浏览 71
已采纳

PHP库函数将多维数组合并为一个基于键的数组? [重复]

This question already has an answer here:

I got two arrays that I would like to combine based on "client_id" key (using PHP function is preferable) :

[all_client] => Array
        (
            [0] => Array
                (
                    [client_id] => 1
                    [client_name] => Thomas Berg
                    [client_phone] => 12313123
                    [client_email] => aaaa@aaa.com
                )

            [1] => Array
                (
                    [client_id] => 2
                    [client_name] => John Doe
                    [client_phone] => 4231241
                    [client_email] => asdas@asdas.com
                )
    )

    [all_client_document] => Array
        (
            [0] => Array
                (
                    [client_document_id] => 3
                    [client_document_number] => BX100
                    [client_document_type] => passport
                    [client_document_issued_date] => 2018-10-17
                    [client_document_expired_date] => 2018-12-02
                    [client_id] => 1
                )

            [1] => Array
                (
                    [client_document_id] => 4
                    [client_document_number] => DJ200
                    [client_document_type] => passport
                    [client_document_issued_date] => 2018-10-15
                    [client_document_expired_date] => 2018-11-23
                    [client_id] => 2
                )

        )

)

How to merge these two array? Any PHP function to achieve this without foreach looping? I would hope to see the result as below :

[new_result] => Array
        (
            [0] => Array
                (
                    [client_id] => 1
                    [client_name] => Thomas Berg
                    [client_phone] => 12313123
                    [client_email] => aaaa@aaa.com
                    [client_document_id] => 3
                    [client_document_number] => BX100
                    [client_document_type] => passport
                    [client_document_issued_date] => 2018-10-17
                    [client_document_expired_date] => 2018-12-02
                )

            [1] => Array
                (
                    [client_id] => 2
                    [client_name] => John Doe
                    [client_phone] => 4231241
                    [client_email] => asdas@asdas.com
                    [client_document_id] => 4
                    [client_document_number] => DJ200
                    [client_document_type] => passport
                    [client_document_issued_date] => 2018-10-15
                    [client_document_expired_date] => 2018-11-23
                )
    )

How can I achieve this? Any help is much appreciated. Thanks in advanced before!

</div>
  • 写回答

2条回答 默认 最新

  • doujing3896 2019-02-08 06:09
    关注

    I found the answer based on the same question :

    $first = array_column($data['all_client'], null, 'client_id');
    $second = array_column($data['all_client_document'], null, 'client_id');
    $result = array_values(array_replace_recursive($first, $second));
    

    It reproduces as what I want also and simpler, thanks anyone!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题