简体   繁体   中英

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.

我不认为有这样一个特殊的本机功能,但你可以自己做:

$array3 = array_map(function ($a, $b) { return $a . $b; }, $array1, $array2);

对不起,没有这样的功能

There is no such native function, but you can do it without the third array, for example

foreach($Array1 as $key=>$value)
  $Array1[$key] .= $Array2[$key];

print_r($Array1);

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