while 循环中的最后一次迭代 [英] the last iteration in a while loop
本文介绍了while 循环中的最后一次迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当迭代输出与字符串不同的内容时,我试图获取数组中的最后一项.
I'm trying to get the last item in an array, when being iterated to output something different to a string.
if (count($this->condition) > 1) {
$i=0;
while ($i < count($this->condition)){
if ($i == (count($this->condition) -1)) {
foreach ($this->condition as $key => $value){
$query .= $value['columns']." = ".$value['value'];
}
} else {
foreach ($this->condition as $key => $value){
$query .= $value['columns']." = ".$value['value']." AND ";
$i++;
}
}
}
} else {
foreach ($this->condition as $key => $value){
$query .= $value['columns']." = ".$value['value'];
}
}
然而,它不断添加 AND,这意味着 $i == (count($this->condition))
永远不会为真.
However, it keeps adding the AND, meaning that $i == (count($this->condition))
is never true.
我该如何解决这个问题?
How do I resolve this?
推荐答案
我已经解决了.
if (count($this->condition) > 1) {
$i=0;
foreach ($this->condition as $key => $value){
if ($i == ($conditionCount -1)) {
$query .= $value['columns']." = ".$value['value'];
} else {
$query .= $value['columns']." = ".$value['value']." AND ";
}
$i++;
}
} else {
foreach ($this->condition as $key => $value){
$query .= $value['columns']." = ".$value['value'];
}
}
这篇关于while 循环中的最后一次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文