Add this to array?

I have this array:

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

    [2] => Array
        (
            [birthdate] => 1985-05-10 00:00:00
            [sex] => 1
        )

    [1914] => Array
        (
            [birthdate] => 1983-02-19 00:00:00
            [sex] => 1
        )

)

How can I take address from array below and add it to array above under it’s corespondent ID

Array
(
    [0] => Array
        (
            [entityid] => 1
            [address] => 21005 Huelva, Huelva, España
        )

    [1] => Array
        (
            [entityid] => 1914
            [address] => Huelva, Huelva, Spain
        )

    [2] => Array
        (
            [entityid] => 2
            [address] => Huelva, Huelva, España
        )

)

Try foreach ($array1 as $key => $value){ foreach ($array2 as $item){ if ($item['entityid'] == $key){ $key['address']=$item['address']; } } }

Thanks buddy very much how do I output it please?

To put it in a readable format, you could do

echo "<pre>";
print_r($array1);
echo "<pre>";

Don’t put this in the loop, put it after the last bracket of the code I gave you. It will print it to the screen.

foreach ($questionList as $key => $value){
foreach ($googlelist as $item){
if ($item['entityid'] == $key){
$key['address']=$item['address'];
echo $key['address'];
}


}
}    

echo "<pre>";
print_r($questionList);
echo "<pre>";    

it returns this address is missing!

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

    [2] => Array
        (
            [birthdate] => 1985-05-10 00:00:00
            [sex] => 1
        )

    [1914] => Array
        (
            [birthdate] => 1983-02-19 00:00:00
            [sex] => 1
        )

)

foreach() works with a copy of the array values.

Could you please post the code with arrays please?

Does echo $key['address'] return anything? You are running that in your loop, what does it print?

Negative nothing is returned!

foreach ($questionList as $key => $value){
foreach ($googlelist as $item){
    echo $key['address'];
if ($item['entityid'] == $key){
$key['address']=$item['address'];
}


}
}

which is expected behaviour for foreach().

$item[‘address’] returns addresses but $key[‘address’] nothing.

I would like to make array1 look like this:

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

    [2] => Array
        (
            [birthdate] => 1985-05-10 00:00:00
            [sex] => 1
            [address] => address
        )

    [1914] => Array
        (
            [birthdate] => 1983-02-19 00:00:00
            [sex] => 1
            [address] => address
        )

)

This will change the contents of the array $questionList but it may be what you want.

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

untested & without validation

foreach (array_column($addresses, 'address', 'entityid') as $id => $address) {
    $data[$id]['address'] = $address;
}

@davidtsadler baby your code works great thanks so much.

Guys all of you thank you very very much for your support really appreciate the time you dedicated solving this issue.
Thanks

@Dormilich’s answer is a much nicer solution.

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