//ads:
?>
Implode columnar values between two arrays into a flat array of concatenated strings
PHP Snippet 1:
// updated version
$a = ['a', 'b', 'c'];
$b = ['d', 'e', 'f', 'g'];
print_r(array_map('implode', array_map(null, $a, $b)));
PHP Snippet 2:
//updated version
$array_A = ['foo', 'bar', 'quuz'];
$array_B = ['baz', 'qux', 'corge'];
for ($i = 0, $c = count($array_A); $i<$c; $i++) {
$result[$i] = $array_A[$i].$array_B[$i];
}
var_dump($result);
PHP Snippet 3:
function array_zip($a1, $a2) {
$out = [];
for($i = 0; $i < min(sizeof($a1), sizeof($a2)); $i++) {
array_push($out, $a1[$i] . $a2[$i]);
}
return $out;
}
PHP Snippet 4:
$a = ["foo", "bar"];
$b = ["baz", "qux"];
print_r(array_zip($a, $b));
PHP Snippet 5:
Array
(
[0] => foobaz
[1] => barqux
)
PHP Snippet 6:
$A = ['foo', 'bar'];
$B = ['baz', 'qux'];
function arraySquish($array)
{
$new = [''];
foreach($array as $val) {
$new[0] .= $val;
}
return $new;
}
$A = arraySquish($A);
$B = arraySquish($B);
echo '<pre>';
print_r($A);
print_r($B);
echo '</pre>';
PHP Snippet 7:
<?php
$colours = ['red', 'white', 'blue'];
$items = ['robin', 'cloud', 'mountain'];
$squished =
array_map(
function($colour, $item) {
return $colour.$item;
},
$colours,
$items
);
var_export($squished);
PHP Snippet 8:
array (
0 => 'redrobin',
1 => 'whitecloud',
2 => 'bluemountain',
)
PHP Snippet 9:
$b = array_intersect_key($b, $a);
$a = array_intersect_key($a, $b);
PHP Snippet 10:
$a = ['a', 'b', 'c'];
$b = ['d', 'e', 'f', 'g'];
var_export(array_map(fn() => implode(func_get_args()), $a, $b));
PHP Snippet 11:
var_export(array_map(fn(...$column) => implode($column), $a, $b));
PHP Snippet 12:
array (
0 => 'ad',
1 => 'be',
2 => 'cf',
3 => 'g',
)