Search code examples
phparraysconcatenationarray-map

Implode columnar values between two arrays into a flat array of concatenated strings


I have two arrays, $array_A and $array_B. I'd like to append the first value from $array_B to the end of the first value of $array_A and repeat this approach for all elements in both arrays.

$array_A = ['foo', 'bar', 'quuz'];
$array_B = ['baz', 'qux', 'corge'];

Expected output after squishing:

['foobaz', 'barqux', 'quuzcorge']

array_merge($array_A, $array_B) simply appends array B onto array A, and array_combine($array_A, $array_B) sets array A to be the key for array B, neither of which I want. array_map seems pretty close to what I want, but seems to always add a space between the two, which I don't want.

It would be ideal for the lengths of each array it to be irrelevant (e.g. array A has five entries, array B has seven entries, extra entries are ignored/trimmed) but not required.


Solution

  • // updated version
    $a = ['a', 'b', 'c'];
    $b = ['d', 'e', 'f', 'g']; 
    print_r(array_map('implode', array_map(null, $a, $b)));