任何人都可以帮我炸掉最后一个逗号吗?
column name = used_receipt
值列在下面的列中......
301,302,303,
现在的问题是,当我尝试从此文本框中获取最小值和最大值并单击“保存”时,for循环因为上一个逗号而进入无限循环。
if(isset($_POST['save'])){
$used_receipt = $_POST['used_receipt'];
$arr = array_filter(explode(",", $used_receipt));
$min = min($arr);
$max = max($arr);
for ($i = $min; $i <= $max; $i++){
$insertrow = $database->insertRow("INSERT INTO book_return (used_receipt) VALUES (:used_receipt)",
array(':used_receipt'=>$i));
}
}
答案 0 :(得分:4)
你犯了两个错误:
1。爆炸声明中有空格
2。您不能从数组中删除空值
所以要摆脱这些错误,你可以这样做:
$arr = array_filter(explode(",", $used_receipt));
//^^^^^^^^^^^^ ^ See only a comma without a space
//| To remove empty values from your array
答案 1 :(得分:1)
rtrim($used_receipt, ',');
这将删除后缀逗号。