PHP循环,改变最后一项?

发布于 2024-10-07 06:07:09 字数 328 浏览 1 评论 0原文

这实际上可能是一个 css 问题,但我希望不是,因为我希望它能在 IE 中工作。

我有以下循环:

<?php 
  if ($category)
{
     foreach($category as $item)
         {
               echo $item['name'];
               echo ", ";
         }
} ?>

应该输出

项目,项目,项目,项目,

唯一的事情是......我不想在最后一个项目之后有逗号。有没有办法在循环内做到这一点?

This might actually be a css question but I'm hoping not because I'd like this to work in IE.

I have the following loop:

<?php 
  if ($category)
{
     foreach($category as $item)
         {
               echo $item['name'];
               echo ", ";
         }
} ?>

Which should output

item, item, item, item,

The only thing is...I'd like to NOT have a comma after the last item. Is there any way to do this within a loop?

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

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

发布评论

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

评论(8

沧笙踏歌 2024-10-14 06:07:09

为了保持代码原样,您可以添加一个计数器,并跳过最后一个。

<?php 
if ($category) {
     $counter = 0;
     foreach($category as $item)
         {
               $counter++;
               echo $item['name'];
               if ($counter < count($category)) {
                   echo ", ";
               }
         }
}
?>

或者你可以做得更多、更多、更快:

<?php echo implode(", ", array_map(create_function('$item', 'return $item["name"];'), $category)); ?>

Well to keep your code how it is, you could add a counter, and skip the last one.

<?php 
if ($category) {
     $counter = 0;
     foreach($category as $item)
         {
               $counter++;
               echo $item['name'];
               if ($counter < count($category)) {
                   echo ", ";
               }
         }
}
?>

Or you can do it much, much, quicker:

<?php echo implode(", ", array_map(create_function('$item', 'return $item["name"];'), $category)); ?>
凉城已无爱 2024-10-14 06:07:09

不要立即回显,而是将输出保存到可以修剪的变量中。

<?php 
if ($category) {
    $output = '';
    foreach($category as $item) {
           $output .= $item['name'];
           $output .= ", ";
    }
    echo rtrim($output, ', ');
} 
?>

Don't echo immediately but save your output into a variable that you can trim.

<?php 
if ($category) {
    $output = '';
    foreach($category as $item) {
           $output .= $item['name'];
           $output .= ", ";
    }
    echo rtrim($output, ', ');
} 
?>
长安忆 2024-10-14 06:07:09

implode 解决方案是最简单的,但您要求一个循环。此方法避免在循环中放置额外的条件,因此应该更有效。基本上,您不是对最后一个项目执行不同的操作,而是对第一个项目执行不同的操作。

$myArray = array(); //Fill with whatever
$result = $myArray[0];
for ($idx = 1; $idx < count($myArray); $idx += 1)
{
    $result .= ', ' . $myArray[$idx];
}

编辑:意识到你想要 $item['name'] 而不仅仅是 $item 后:

$myArray = array(); //Fill with whatever
$result = $myArray[0]['name'];
for ($idx = 1; $idx < count($myArray); $idx += 1)
{
    $result .= ', ' . $myArray[$idx]['name'];
}

The implode solution is the simplest, but you asked for a loop. This method avoids putting an extra conditional in the loop, and therefore should be somewhat more efficient. Basically, instead of doing something different for the last item, you do something different for the first item.

$myArray = array(); //Fill with whatever
$result = $myArray[0];
for ($idx = 1; $idx < count($myArray); $idx += 1)
{
    $result .= ', ' . $myArray[$idx];
}

EDIT: After realizing you want $item['name'] instead of just $item:

$myArray = array(); //Fill with whatever
$result = $myArray[0]['name'];
for ($idx = 1; $idx < count($myArray); $idx += 1)
{
    $result .= ', ' . $myArray[$idx]['name'];
}
左秋 2024-10-14 06:07:09

尽管 foreach 很可爱,...

<?php
if ($category) {
     $count = count($category) - 1;
     for ($i = 0; $i <= $count; $i++) {
         echo $category[$i]['name'];
         if ($i < $count)
             echo ', ';
     }
}
?>

... for 有时是必要的。

As lovely as foreach is,...

<?php
if ($category) {
     $count = count($category) - 1;
     for ($i = 0; $i <= $count; $i++) {
         echo $category[$i]['name'];
         if ($i < $count)
             echo ', ';
     }
}
?>

...for is sometimes necessary.

青朷 2024-10-14 06:07:09

假设 $category 是一个数组,您可以使用 implode 得到你想要的:

编辑: 错过了 $categories['name'] 部分,这应该有效:

<?php implode(", ", array_keys($category, 'name')); ?>

Assuming $category is an array, you can use implode to get what you want:

Edit: Missed the $categories['name'] part, this should work:

<?php implode(", ", array_keys($category, 'name')); ?>
聊慰 2024-10-14 06:07:09

“最后一个逗号”问题的标准解决方案是将项目放入数组中,然后将其内爆:

 $temp = array();
 foreach($category as $item)
    $temp[] =  $item['name'];
 echo implode(', ', $temp);

如果您想要更通用,您还可以编写一个函数,从每个子数组中挑选(“抽取”)特定字段:

  function array_pluck($ary, $key) {
     $r = array();
     foreach($ary as $item)
         $r[] = $item[$key];
     return $r;
  }

然后就

  echo implode(', ', array_pluck($category, 'name'));

The standard solution to the "last comma" problem is to put items into an array and then implode it:

 $temp = array();
 foreach($category as $item)
    $temp[] =  $item['name'];
 echo implode(', ', $temp);

If you want this more generic, you can also write a function that picks ("plucks") a specific field out of each subarray:

  function array_pluck($ary, $key) {
     $r = array();
     foreach($ary as $item)
         $r[] = $item[$key];
     return $r;
  }

and then just

  echo implode(', ', array_pluck($category, 'name'));
画离情绘悲伤 2024-10-14 06:07:09

或者你可以检查最后一个键:

end($category);
$lastkey = key($category);
foreach ( $category AS $key => $item ) {
  echo $item['name'];
  if ( $lastkey != $key ) {
    echo ', ';
  }
}

Or you could check for the last key:

end($category);
$lastkey = key($category);
foreach ( $category AS $key => $item ) {
  echo $item['name'];
  if ( $lastkey != $key ) {
    echo ', ';
  }
}
甜宝宝 2024-10-14 06:07:09

还有另一个选择:

<?php 
$out = "";

foreach ($category as $item)
    {
    $out .= $item['name']. ", ";
    }

$out = preg_replace("/(.*), $/", "$1", $out);
echo $out;
?>

And another option:

<?php 
$out = "";

foreach ($category as $item)
    {
    $out .= $item['name']. ", ";
    }

$out = preg_replace("/(.*), $/", "$1", $out);
echo $out;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文