Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function for concating matching index strings in array?

Alright, I know it can be done like so...

$Array1 = array("Hello, ", "foo", "The quick brown fox jumped ");
$Array2 = array("World!", "bar", "the lazy dog.");
$Array3 = array();
for($x = 0; $x < count($Array1); $x++) {
    $Array3[] = $Array1[$x] . $Array2[$x];
}
// returns this -> ("Hello, World", "foobar", "The quick brown fox jumped over the lazy dog.")

But is there a native PHP function for this? Like this:

$Array3 = array_concat_strings($Array1, $Array2)

I searched through php.net, and didn't find anything, but I like to be sure I'm not missing something.

like image 909
Yoshiyahu Avatar asked Dec 04 '25 02:12

Yoshiyahu


1 Answers

I don't think there's such a particular native function, but you can make your own:

$array3 = array_map(function ($a, $b) { return $a . $b; }, $array1, $array2);
like image 58
deceze Avatar answered Dec 05 '25 15:12

deceze



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!