简体   繁体   English

如何使用逗号组合数组中的所有元素?

[英]How do I combine all elements in an array using comma?

I know this question has with out any doubt been asked a whole lot of times. 我知道这个问题已经被问到了很多次。 I though can seem to find a solution. 我似乎可以找到解决方案。 So forgive me if it's way to simple. 如果这是简单的方法,请原谅我。

The question is how do access the end of a while loop. 问题是如何访问while循环的结束。

Eg 例如

    while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
            $c = $countByMonth["COUNT(id)"];
            echo $c . "," ; 
        }

How do I manage separate each value of the while loop by a comma but of course I don't want the comma at the end of the value. 如何通过逗号分隔while循环的每个值,但当然我不希望逗号位于值的末尾。

In advance thank you very much for your help :) 提前谢谢你的帮助:)

You can: 您可以:

1) Build a string, and remove the last character: 1)构建一个字符串,并删除最后一个字符:

$c = '';
while ($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c .= $countByMonth["COUNT(id)"] . ',';
}

$c = substr($c, 0, -1);
echo $c;

2) Build an array and use implode() 2)构建一个数组并使用implode()

$c = array();
while ($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c[] = $countByMonth["COUNT(id)"];
}

echo implode(',', $c);

Tip: You can use aliases in your query, like: SELECT COUNT(id) as count FROM ... . 提示:您可以在查询中使用别名,例如: SELECT COUNT(id) as count FROM ... Then you can access it as $countByMonth['count'] , which looks cleaner IMO. 然后您可以将其作为$countByMonth['count'] ,这看起来更清晰IMO。

The simple 1 solution: 简单的1解决方案:

$isFirst = true;
while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c = $countByMonth["COUNT(id)"];
    if ($isFirst) {
        $isFirst = false;
    } else {
        echo = ', ';
    }
    echo $c; 
}

Alternatively, you could implode() the values. 或者,您可以implode()值。 Or - perhaps easier to read/understand/maintain - concatenate it all into a string and remove the last " , " (SO eats my whitespace; the string is comma-whitespace): 或者 - 也许更容易阅读/理解/维护 - 将它全部连接成一个字符串并删除最后一个“ , ”(这就是我的空格;字符串是逗号 - 空格):

$list = '';
while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
    $c = $countByMonth["COUNT(id)"];
    $list .= $c . ', '; 
}
echo substring($list, 0, -2); // Remove last ', '

(Several other answers propose the use of an accumulated array and then use implode() . From a performance perspective this method will be superior to string concatenation.) (其他几个答案建议使用累积数组,然后使用implode() 。从性能角度来看,这种方法优于字符串连接。)

1 See comments. 1见评论。

Alternatively you can do: 或者你可以这样做:

$arr = array();
while($countByMonth = mysql_fetch_array($countByMonthSet)) { 
   $arr[] = $countByMonth["COUNT(id)"];
}

echo implode(', ',$arr);

或者之后用rtrim($ c,',')修剪它

While I think the implode solution is probably best, in situations where you can't use implode, think of the basic algorithm differently. 虽然我认为内爆解决方案可能是最好的,但在不能使用内爆的情况下,请考虑不同的基本算法。 Rather than "how can I add a comma behind every element but the last?" 而不是“除了最后一个元素,我怎么能在每个元素后面添加一个逗号?” ask yourself "how can I add a comma before every element but the first?" 问问自己“除了第一个元素之外,我怎样才能在每个元素之前添加一个逗号?”

$str = '';
$count = count( $array );
if( $count ) {
  $i = 0;
  $str = $array[$i];
  $i++;
  while( i < $count ) {
    $str .= ','.$array[$i];
    $i++;
  }
}

If you "shift" the first element, then you can use a foreach loop: 如果你“移动”第一个元素,那么你可以使用foreach循环:

$str = '';
if( count( $array ) ) {
  $str = array_shift( $array );
  foreach( $array as $element ) {
    $str .= ', '.$element;
  }
}

Ty this: 这个:

int count;//

while(i)
{
    count=i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM