Merge these arrays?

Array1
(
    [1] => Array
        (
            [birthdate] => 1983-05-21 00:00:00
            [sex] => 1
        )
)

Array2
(
    [1] => Array
        (
            [realname] => Victor Dub
)
)

In these arrays 1 co responds to user ID. How do I take realname from array2 and add to array1 one making sure the ID is the same?

To make it look like this

 Array1
    (
        [1] => Array
            (
                [birthdate] => 1983-05-21 00:00:00
                [sex] => 1
                [realname] => Victor Dub
            )
    )
$merged = [
    1 => $one[1] + $two[1]
];

Thanks buddy.
This 1 I mentioned is not always the same. Array has hundreds of ID!

For instance

Array2
(
    [1] => Array
        (
            [realname] => Victor Dub
)
[55] => Array
        (
            [realname] => x
)
)

your array structure is invalid. just use foreach on the first array to get the key, then array_merge with the same key from the second array.

Could you guys help me out with the code I have spent hours with no result.

Check out array merge or array push on php.net

this looks like the arrays come off a DB. so chances are that you can create a query that gives you the desired output directly without the need to merge arrays in PHP.

I need to foreach array2 to then check array1 if has same ID then add value with same ID from array2 to array1.

Please help me out with code.

We don’t know your DB, so at most I can guess that a JOIN will be involved.

Guys no DB is needed here.

some like this:

foreach ($googlelist as $item) {
    if(array_key_exists($item['entityid'], $questionList)) {
        $questionList[$item['entityid']]['address'] = $item['address'];
}

Just applied to arrays above.

so where do you get the data from, if not from a DB?

I already have it in array! I use already prebuild functions can’t mess with DB!

create new & better prebuild functions?

Could you just please try to help me out with foreach?

Could you just please try it on your own in the first place? that’s just one or two lines of code by my instructions.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.