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条)

相关推荐 更多相似问题