如何在PHP中从数组中创建逗号分隔的列表?
我知道如何使用foreach循环遍历数组项并附加逗号,但去掉最后一个逗号总是一件痛苦的事情。有没有一种简单的PHP方法如何在PHP中从数组中创建逗号分隔的列表?,php,arrays,Php,Arrays,我知道如何使用foreach循环遍历数组项并附加逗号,但去掉最后一个逗号总是一件痛苦的事情。有没有一种简单的PHP方法 $fruit = array('apple', 'banana', 'pear', 'grape'); 最终我想要 $result = "apple, banana, pear, grape" 你想用这个 即: $commaList=内爆(',',$fruit) 有一种方法可以附加逗号,而不必使用尾随逗号。如果您必须同时执行其他操作,您可能会希望执行此操作。例如,您可能希
$fruit = array('apple', 'banana', 'pear', 'grape');
最终我想要
$result = "apple, banana, pear, grape"
你想用这个
即:
$commaList=内爆(',',$fruit)代码>
有一种方法可以附加逗号,而不必使用尾随逗号。如果您必须同时执行其他操作,您可能会希望执行此操作。例如,您可能希望引用每个水果,然后用逗号将它们分开:
$prefix = $fruitList = '';
foreach ($fruits as $fruit)
{
$fruitList .= $prefix . '"' . $fruit . '"';
$prefix = ', ';
}
此外,如果您只是以“正常”的方式在每个项目后添加逗号(听起来像您之前所做的那样),并且需要删除最后一个项目,只需执行$list=rtrim($list,,')
。在这种情况下,我看到很多人不必要地到处乱搞substr
。我更喜欢在FOR循环中使用IF语句来检查以确保当前迭代不是数组中的最后一个值。如果不是,请添加逗号
$fruit = array("apple", "banana", "pear", "grape");
for($i = 0; $i < count($fruit); $i++){
echo "$fruit[$i]";
if($i < (count($fruit) -1)){
echo ", ";
}
}
$fruit=数组(“苹果”、“香蕉”、“梨”、“葡萄”);
对于($i=0;$i
我就是这样做的:
$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
现场演示:
编辑:根据@joseantgv的评论,您应该能够从上述示例中删除rtrim()
。即:
$string = implode(',', $arr);
如果你做引用答案,你可以做
$commaList = '"'.implode( '" , " ', $fruit). '"';
以上假设果为非空。如果您不想做出这样的假设,可以使用If-then-else语句或三元(?:)运算符。另一种方法可以如下所示:
$letters = array("a", "b", "c", "d", "e", "f", "g");
$result = substr(implode(", ", $letters), 0, -3);
$fruit = array('apple', 'banana', 'pear', 'grape');
$sep = ',';
array_reduce(
$fruits,
function($fruitsStr, $fruit) use ($sep) {
return (('' == $fruitsStr) ? $fruit : $fruitsStr . $sep . $fruit);
},
''
);
$result
的输出是一个格式良好的逗号分隔列表
a, b, c, d, e, f, g
与Lloyd的答案类似,但适用于任何大小的阵列
$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."
}
$missing=array();
$missing[]='name';
$missing[]='zipcode';
$missing[]=“电话”;
如果(是_数组($missing)&&count($missing)>0)
{
$result='';
$total=计数($missing)-1;
对于($i=0;$i 0)
$result.=“和”;
$result.=$missing[$i];
如果($i<$total)
$result.=“,”;
}
echo“您需要提供您的“$result”。”;
//Echos“您需要提供您的姓名、密码和电话。”
}
有时,在某些情况下,您甚至不需要php来实现此目的(例如,列表项在渲染时都在其自己的通用标记中),如果从脚本渲染后它们是独立的元素,则始终可以通过css向所有元素添加逗号,但最后一个子元素除外
我在主干应用程序中经常使用它,实际上是为了修剪一些任意代码:
.likers a:not(:last-child):after { content: ","; }
基本上看元素,除了它的最后一个元素之外,其他元素都是目标,并且在每个项目后面添加一个逗号。如果情况适用,这只是一种完全不必使用脚本的替代方法
$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结果,最后是和:
$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
功能解决方案如下所示:
$letters = array("a", "b", "c", "d", "e", "f", "g");
$result = substr(implode(", ", $letters), 0, -3);
$fruit = array('apple', 'banana', 'pear', 'grape');
$sep = ',';
array_reduce(
$fruits,
function($fruitsStr, $fruit) use ($sep) {
return (('' == $fruitsStr) ? $fruit : $fruitsStr . $sep . $fruit);
},
''
);
跟着这个
$teacher_id = '';
for ($i = 0; $i < count($data['teacher_id']); $i++) {
$teacher_id .= $data['teacher_id'][$i].',';
}
$teacher_id = rtrim($teacher_id, ',');
echo $teacher_id; exit;
$teacher\u id='';
对于($i=0;$i
调用$fruitlist=''”(如果尝试添加引号)会更有意义如果希望在将数组中的数据转换为字符串之前对其进行操作,则foreach循环更好;例如,addslashes或mysql\u real\u escape\u string。如果总计数与4不同?如上所述,您可以在joseantgv上执行内爆(“,”,$arr)@joseantgv您是对的,我不知道为什么要使用rtrim()
。我记得有一个问题,字符串的末尾有多余的逗号,但我记不起发生的情况。最简单的答案是。当你能在一行代码中完成它时,为什么还要写几十行呢。