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 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法