What I want to achieve is the following:
transform an array:
['A','B','C'];
into a string like:
"A is A, B is B, C is C"
Is there any other way than a foreach-loop (like implode) to achieve this?
If you're not looking to loop through, you could use array_walk() function.
Try this:
$input = ['A','B','C'];
function test_alter(&$item, &$key) {
$item = $item.' is '.$item;
}
array_walk($input, 'test_alter');
echo implode(', ', $input);
Output:
A is A, B is B, C is C
$arr = ['A','B','C'];
function myfunction($arr)
{
return "$arr is $arr";
}
print_r(array_map("myfunction",$arr));
//[0] => A is A [1] => B is B [2] => C is C
echo implode(', ', array_map(function($v){ return "$v is $v"; }, $arr));use below way with array_walk
$arr = ['A','B','C'];
function _print($item2, $key)
{
echo "$item2 is $item2, ";
}
array_walk($arr, '_print');
or if need to save in string use below
$arr = ['A','B','C'];
$str = '';
function _print($item2, $key)
{
global $str;
$str = $str."$item2 is $item2, ";
}
array_walk($arr, '_print');
echo trim($str,',');
global declaration should only be used when no other method is suitable -- as shown by other answers, global is not needed to generate the desired output string. Generally, you would just pass the $str value as a function argument (and make it modifiable by reference).Here's my recommendation, since you're accustomed to using implode in php, I wrote you a duplicate function that takes a formatter element in your separator. Anywhere you put %x will duplicate the array key like so:
echo implodeDuplicate(" is %x, ", $myArray);
assuming $myArray was:
$myArray = array(1,2,3,4);
it will output:
1 is 1, 2 is 2, 3 is 3, 4 is 4,
Here's the function:
function implodeDuplicate($seperator, $array) {
$finalString = "";
for($i = 0; $i < count($array); $i++) {
$trueSep = str_replace("%x", $array[$i], $seperator);
$finalString .= $array[$i] . $trueSep;
}
return $finalString;
}
Hope it's helpful! :)
I see array_map() and array_walk() solutions, but no one posted an array_reduce() method, so I will for completeness.
For small arrays (few iterations required), array_map() will execute ever-so-slightly faster (your users will never notice the difference).
As the input array increases in size, array_reduce() takes the lead and distances itself from array_map(). Do your own benchmarks with your own real data if you need to perform micro-optimization.
array_reduce() on 26-element array looped 100 times: https://3v4l.org/1oCXM/perf#output
array_map() on 26-element array looped 100 times: https://3v4l.org/WcjMB/perf#output
My following method doesn't need implode() because array_reduce() outputs a string, however it does need a conditional on each iteration to avoid "over-gluing".
Code: (Demo)
$input = ['A', 'B', 'C'];
echo array_reduce(
$input,
fn($result, $v) => ($result ? "$result, " : '') . "$v is $v"
);
Or here is array_map() with arrow function syntax then implode(): (Demo)
echo implode(
', ',
array_map(
fn($v) => "$v is $v",
$input
)
);
Output:
A is A, B is B, C is C
array_map()orarray_reduce().