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'];
}
}