我使用下面的代码,
$i = 0; $comma = NULL;
foreach($messages->get_logged_agents_dep(60) as $val_dep)
{
echo $department_id = $val_dep['department_id'].$comma;
}
我想把逗号(,)结束于department_id(就像我在$逗号变量中提到的那样) 但是最后一个值不应该放入逗号(,),因为我想把上面的值放到SELECT * FROM tb_name WHERE IN(1,2,3)中这样, 请帮我解决这个问题。 尊重你的回应。
谢谢!
答案 0 :(得分:5)
试试这个: -
foreach($messages->get_logged_agents_dep(60) as $val_dep)
{
$department_id[] = $val_dep['department_id'];
}
echo $department_val = implode(",",$department_id);
答案 1 :(得分:1)
更改代码,在序列的其他成员上添加逗号,而不是。
$i = 0;
$comma = ',';
foreach(...)
{
echo ($i++>0)? $comma:'';
echo $department_id = $val_dep['department_id'];
}
答案 2 :(得分:1)
您也可以使用rtrim()
$department_id = '';
foreach($messages->get_logged_agents_dep(60) as $val_dep) {
$department_id .= $val_dep['department_id'].',';
}
rtrim($department_id,','); // to trim last comma
答案 3 :(得分:-1)
使用implode()。见http://php.net/manual/en/function.implode.php
$i = 0; $arrIds = array();
foreach($messages->get_logged_agents_dep(60) as $val_dep)
{
$arrIds[] = $val_dep['department_id'];
}
echo implode(',', $arrIds);