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>

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!

You can achieve this by using array_map() function:

function make_my_data($a, $b)
{   
    return array_merge($a,$b);
}

$a = [
    0 => [
        'user_id' => 1,
        'user_name' => "Ram",
    ],
    1 => [
        'user_id' => 2,
        'user_name' => "Raj",
    ]
];

$b = [
    0 => [
        'user_id' => 1,
        'user_emai' => "ram@abc.com",
    ],
    1 => [
        'user_id' => 2,
        'user_email' => "raj@abc.com",
    ]
];


$d = array_map("make_my_data", $a , $b);
echo "<pre>"; print_r($d);

Output:

Array
(
    [0] => Array
        (
            [user_id] => 1
            [user_name] => Ram
            [user_emai] => ram@abc.com
        )

    [1] => Array
        (
            [user_id] => 2
            [user_name] => Raj
            [user_email] => raj@abc.com
        )

)