how do I concatenate the string values of two arrays pairwise with PHP?

后端 未结 9 1317
傲寒
傲寒 2020-12-21 00:43

So I have two arrays

Array
(
[0] => test
[1] => test 1
[2] => test 2
[3] => test 3
)

and

Array
(
[0] => test         


        
9条回答
  •  旧时难觅i
    2020-12-21 01:29

    There's no built-in function (that I know of) to accomplish that. Use a loop:

    $combined = array();
    for($i = 0, $l = min(count($a1), count($a2)); $i < $l; ++$i) {
      $combined[$i] = $a1[$i] . $a2[$i];
    }
    

    Adapt the loop to your liking: only concatenate the minimum number of elements, concatenate empty string if one of the arrays is shorter, etc.

提交回复
热议问题