2010-03-12 76 views
88

我知道如何使用foreach循環訪問一個數組的項目並添加一個逗號,但總是需要取消最後一個逗號。有沒有一個簡單的PHP方法來做到這一點?如何在PHP中創建逗號分隔列表中的數組?

$fruit = array('apple', 'banana', 'pear', 'grape'); 

最後我想

$result = "apple, banana, pear, grape" 

回答

186

你想用implode這一點。

即: $commaList = implode(', ', $fruit);


有追加逗號不具有後一個的方法。如果你必須同時做一些其他操作,你會想這樣做。比如,也許你想用逗號引用每種水果,然後將它們分開所有:

$prefix = $fruitList = ''; 
foreach ($fruits as $fruit) 
{ 
    $fruitList .= $prefix . '"' . $fruit . '"'; 
    $prefix = ', '; 
} 

此外,如果你只是做了每個項目後附加一個逗號(像它聽起來你是「正常」的方式之前做),你需要修剪最後一個,只是做$list = rtrim($list, ', ')。在這種情況下,我看到很多人不必要地使用substr

+3

這將使更多的仙se(如果試圖添加引號)調用$fruitlist = '"' . implode('", "', $fruit) . '"'; cwallenpoole 2010-03-12 19:39:14

+0

如果要在數組中處理數據之前將foreach循環放入字符串中,foreach循環會更好;例如,addslashes或mysql_real_escape_string。 – 2015-08-21 15:48:53

3

我更喜歡在FOR循環中使用IF語句,它檢查以確保當前迭代不是數組中的最後一個值。如果沒有,添加逗號

$fruit = array("apple", "banana", "pear", "grape"); 

for($i = 0; $i < count($fruit); $i++){ 
    echo "$fruit[$i]"; 
    if($i < (count($fruit) -1)){ 
     echo ", "; 
    } 
} 
+2

如果總數與4不一樣? – Jyothish 2013-07-17 11:37:29

+0

鍵入1000000而不是3.然後問題解決了嗎? – 2016-01-12 10:21:54

30

這是我怎麼一直在做:

$arr = array(1,2,3,4,5,6,7,8,9); 

$string = rtrim(implode(',', $arr), ','); 

echo $string; 

輸出:

1,2,3,4,5,6,7,8,9 

現場演示:http://ideone.com/EWK1XR

編輯: Per @ joseantgv的評論,你應該能夠移除rtrim()從上面的例子。即:上述

$string = implode(',', $arr); 
+5

如上所述,你可以做implode(',',$ arr) – joseantgv 2015-07-01 15:15:44

+0

@joseantgv你說得對,我不知道我爲什麼使用'rtrim()'。我記得在字符串末尾有額外的逗號時出現問題,但不記得發生的情況。 – Nate 2015-07-04 00:29:57

-1

如果做引述的答案,你可以做

$commaList = '"'.implode('" , " ', $fruit). '"'; 

假定果非空。如果你不想做這個假設,你可以使用if-then-else語句或三元(?:)運算符。

-2

另一種方式可能是這樣的:

$letters = array("a", "b", "c", "d", "e", "f", "g"); 

$result = substr(implode(", ", $letters), 0, -3); 

$result輸出是一個很好的格式化的逗號分隔的列表。

a, b, c, d, e, f, g 
2

與勞埃德的答案類似,但適用於任何大小的數組。

$missing = array(); 
$missing[] = 'name'; 
$missing[] = 'zipcode'; 
$missing[] = 'phone'; 

if(is_array($missing) && count($missing) > 0) 
     { 
      $result = ''; 
      $total = count($missing) - 1; 
      for($i = 0; $i <= $total; $i++) 
      { 
       if($i == $total && $total > 0) 
        $result .= "and "; 

       $result .= $missing[$i]; 

       if($i < $total) 
       $result .= ", "; 
      } 

      echo 'You need to provide your '.$result.'.'; 
      // Echos "You need to provide your name, zipcode, and phone." 
     } 
1

有時候,你甚至不需要PHP這在某些情況下(每個列表項目是在渲染例如自己的通用標籤),您可以通過CSS如果所有元素,但最後孩子隨時添加逗號它們是從腳本渲染後的單獨元素。

我用這個有很多的骨幹實際應用程式削減一些任意代碼脂肪:

.likers a:not(:last-child):after { content: ","; } 

基本上着眼於元,目標都只是它的最後一個元素,每個項目後,它增加了一個逗號。如果情況適用,只需一種替代方法就不必使用腳本。

-2
$letters = array("a", "b", "c", "d", "e", "f", "g"); // this array can n no. of values 
$result = substr(implode(", ", $letters), 0); 
echo $result 

輸出 - > A,B,C,d,E,F,G

3

對於誰願意與端部可以用下面的代碼and導致顯影劑:

$titleString = array('apple', 'banana', 'pear', 'grape'); 
$totalTitles = count($titleString); 
if($totalTitles>1) 
{ 
    $titleString = implode(', ' , array_slice($titleString,0,$totalTitles-1)) . ' and ' . end($titleString); 
} 
else 
{ 
    $titleString = implode(', ' , $titleString); 
} 

echo $titleString; // apple, banana, pear and grape 
0
$fruit = array('apple', 'banana', 'pear', 'grape');  
$commasaprated = implode(',' , $fruit); 
0

功能性的解決辦法是這樣的:

$fruit = array('apple', 'banana', 'pear', 'grape'); 
$sep = ','; 

array_reduce(
    $fruits, 
    function($fruitsStr, $fruit) use ($sep) { 
     return (('' == $fruitsStr) ? $fruit : $fruitsStr . $sep . $fruit); 
    }, 
    '' 
);