如何内爆 foreach

发布于 2024-09-17 09:11:12 字数 247 浏览 7 评论 0原文

如何用逗号内爆foreach()

foreach($names as $name) {
    //do something
    echo '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}

想要在每个链接后添加逗号(最后一个链接除外)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

许一世地老天荒 2024-09-24 09:11:12

Raveren 的解决方案高效且美观,但这里还有另一个解决方案(在类似场景中可能很有用):

$elements = array();
foreach($names as $name) {
    //do something
    $elements[] = '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
echo implode(',', $elements);

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);
最偏执的依靠 2024-09-24 09:11:12

您需要转换数组而不是使用 foreach 进行迭代。您可以使用 array_map

带有闭包的 PHP 5.3 语法

echo implode(", ", array_map(function($name) use($url, $title)
{
    return '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}, $names));

兼容 PHP 5.3 之前的语法

function createLinkFromName($name)
{
    return '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>';
}
echo implode(", ", array_map('createLinkFromName', $names));

具有更好可靠性的 PHP 5.3 语法

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>';
}));

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>';
}));
青衫儰鉨ミ守葔 2024-09-24 09:11:12
$first = TRUE;
foreach($names as $name) {
    //do something
    if(!$first) { echo ', '; }
    $first = FALSE;
    echo '<a href="', $url, '" title="', $title, '">', $name, '</a>';
}
$first = TRUE;
foreach($names as $name) {
    //do something
    if(!$first) { echo ', '; }
    $first = FALSE;
    echo '<a href="', $url, '" title="', $title, '">', $name, '</a>';
}
当爱已成负担 2024-09-24 09:11:12
$s = '';
foreach ($names as $name) {
  if ($s) $s .= ', ';
  $s .= '<a href="' . $url . '" title="' . $title . '">' . $name . '</a>';
}
$s = '';
foreach ($names as $name) {
  if ($s) $s .= ', ';
  $s .= '<a href="' . $url . '" title="' . $title . '">' . $name . '</a>';
}
你穿错了嫁妆 2024-09-24 09:11:12
foreach($names as $name) {
    //do something
    $str .= '<a href="' . $url . '" title="' . $title . '">' . $name .'</a>,';
}
echo substr($str,0,-1);

编辑:
正如评论所指出的,如果您更改分隔符(确切地说是其长度)并忘记 substr 参数,这种做法很容易出错。因此,除非性能绝对关键,否则请使用 foreach 方法。

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.

沉溺在你眼里的海 2024-09-24 09:11:12

这是一个使用 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>';
 }

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>';
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文