How to implode foreach()
with comma?
foreach($names as $name) {
//do something
echo '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
Want to add comma after each link, except the last one.
How to implode foreach()
with comma?
foreach($names as $name) {
//do something
echo '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
Want to add comma after each link, except the last one.
Raveren's solution is efficient and beautiful, but here is another solution too (which can be useful in similar scenarios):
$elements = array();
foreach($names as $name) {
//do something
$elements[] = '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
echo implode(',', $elements);
$elements[] = "<a href=\"$url\" title=
... –
Fancyfree You need to transform your array instead of iterating using foreach. You can do this with array_map
.
PHP 5.3 syntax with closures
echo implode(", ", array_map(function($name) use($url, $title)
{
return '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}, $names));
Compatible syntaxe before PHP 5.3
function createLinkFromName($name)
{
return '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
echo implode(", ", array_map('createLinkFromName', $names));
PHP 5.3 syntax with a better reability
function a_map($array, $function)
{
return array_map($function, $array);
}
echo implode(", ", a_map($names, function($name) use($url, $title)
{
return '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}));
$url
and $title
. They won't be set (at least in the before PHP 5.3 example). –
Empyreal $first = TRUE;
foreach($names as $name) {
//do something
if(!$first) { echo ', '; }
$first = FALSE;
echo '<a href="', $url, '" title="', $title, '">', $name, '</a>';
}
foreach($names as $name) {
//do something
$str .= '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>,';
}
echo substr($str,0,-1);
EDIT: as the comments point out, this way of doing things is a little error prone if you change the separator (precisely its length) and forget the substr parameter. So use the foreach method unless performance is absolutely critical.
substr
. –
Furniture substr
ing it into syntax errordom. I am trying to discourage this approach when there is a cleaner way with implode
. –
Furniture >So use the foreach method unless performance is absolutely critical.
Please recall the downvote. –
Kovacs $s = '';
foreach ($names as $name) {
if ($s) $s .= ', ';
$s .= '<a href="' . $url . '" title="' . $title . '">' . $name . '</a>';
}
if(!$s)
, or better if(strlen($s) == 0) $s = ', ';
–
Reprehensible if ($s)
is correct. We don't want to start with a comma. We want to add one only from the second link on. –
Concourse Here is an ugly solution using echo:
$total = (count($names) - 1 );
foreach($names as $i => $name)
{
if($i != $total)
echo '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>, ';
else
echo '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
$numOfElements = count($names) - 1; foreach ...
)... –
Sowers © 2022 - 2024 — McMap. All rights reserved.