简体   繁体   中英

Append one array to another array with same key

I have two arrays

$array1 = array(33=>'abc,bcd,cde,def');
$array2 = array(33=>'fgh,ghi,hij,ijk');

How I can add two arrays to get the below result?

$array3 = array(33=>'abc,bcd,cde,def,fgh,ghi,hij,ijk');

Thanks in advance...

I presume you want to do this automatically for multiple keys. Try something like this:

$newArray = array();
foreach ($array1 as $key => $value) {
    $newArray[$key] = $array1[$key] . ',' . $array2[$key];
}

Keep in mind you will need checks to see if the data is in both arrays if they do not exactly match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM