0

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] => [email protected]
                )

            [1] => Array
                (
                    [client_id] => 2
                    [client_name] => John Doe
                    [client_phone] => 4231241
                    [client_email] => [email protected]
                )
    )

    [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] => [email protected]
                    [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] => [email protected]
                    [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.

6
  • 3
    Have you tried anything? a loop and merging contents for same index will work. Commented Feb 8, 2019 at 5:40
  • I just want to avoid using foreach loop, any built-in PHP function to achieve this? Commented Feb 8, 2019 at 5:44
  • 1
    You are going to have to use the dreaded foreach loop. Commented Feb 8, 2019 at 5:48
  • 1
    @oitoit why what's wrong with using foreach AFAIK there's no function that can do that Commented Feb 8, 2019 at 5:49
  • the data is quite large, so i think using foreach tends to be slow. I still using it though, just asking whether there is any function for this which might be faster Commented Feb 8, 2019 at 5:53

2 Answers 2

0

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!

Sign up to request clarification or add additional context in comments.

1 Comment

If you got the answer for the same question which was posted earlier, you can delete your question right?
0

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' => "[email protected]",
    ],
    1 => [
        'user_id' => 2,
        'user_email' => "[email protected]",
    ]
];


$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] => [email protected]
        )

    [1] => Array
        (
            [user_id] => 2
            [user_name] => Raj
            [user_email] => [email protected]
        )

)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.