我正在使用一些参数编写SQL查询创建器.在Java中,只需使用数组长度检查当前数组位置,就可以非常轻松地从for循环内部检测数组的最后一个元素.
for(int i=0; i< arr.length;i++){
boolean isLastElem = i== (arr.length -1) ? true : false;
}
Run Code Online (Sandbox Code Playgroud)
在PHP中,它们具有非整数索引来访问数组.因此,您必须使用foreach循环遍历数组.当你需要做出一些决定时(在我的情况下,在构建查询时附加或/和参数),这就成了问题.
我相信必须有一些标准的方法来做到这一点.
你是如何在PHP中解决这个问题的?
Ric*_*eur 289
听起来你想要这样的东西:
$numItems = count($arr);
$i = 0;
foreach($arr as $key=>$value) {
if(++$i === $numItems) {
echo "last index!";
}
}
Run Code Online (Sandbox Code Playgroud)
话虽这么说,你没有 - foreach
在PHP中使用php 迭代"数组" .
Jer*_*ten 197
您可以使用数组的最后一个键的值end(array_keys($array))
并将其与当前键进行比较:
$last_key = end(array_keys($array));
foreach ($array as $key => $value) {
if ($key == $last_key) {
// last element
} else {
// not last element
}
}
Run Code Online (Sandbox Code Playgroud)
小智 40
为什么这么复杂?
foreach($input as $key => $value) {
$ret .= "$value";
if (next($input)==true) $ret .= ",";
}
Run Code Online (Sandbox Code Playgroud)
这将在除最后一个之外的每个值后面添加一个!
OIS*_*OIS 23
当toEnd达到0时,表示它处于循环的最后一次迭代.
$toEnd = count($arr);
foreach($arr as $key=>$value) {
if (0 === --$toEnd) {
echo "last index! $value";
}
}
Run Code Online (Sandbox Code Playgroud)
在循环之后,最后一个值仍然可用,因此如果您只想在循环之后将其用于更多内容,则更好:
foreach($arr as $key=>$value) {
//something
}
echo "last index! $key => $value";
Run Code Online (Sandbox Code Playgroud)
如果您不想将最后一个值视为特殊的内部循环.如果您有大型数组,这应该更快.(如果在同一范围内的循环之后重用数组,则必须先"复制"数组).
//If you use this in a large global code without namespaces or functions then you can copy the array like this:
//$array = $originalArrayName; //uncomment to copy an array you may use after this loop
//end($array); $lastKey = key($array); //uncomment if you use the keys
$lastValue = array_pop($array);
//do something special with the last value here before you process all the others?
echo "Last is $lastValue", "\n";
foreach ($array as $key => $value) {
//do something with all values before the last value
echo "All except last value: $value", "\n";
}
//do something special with the last value here after you process all the others?
echo "Last is $lastValue", "\n";
Run Code Online (Sandbox Code Playgroud)
并回答你的原始问题"在我的情况下,在构建查询时附加或/和参数"; 这将遍历所有值,然后将它们连接在一起,在它们之间加上"和",但不是在第一个值之前或最后一个值之后:
$params = [];
foreach ($array as $value) {
$params[] = doSomething($value);
}
$parameters = implode(" and ", $params);
Run Code Online (Sandbox Code Playgroud)
hak*_*kre 20
已经有很多答案,但是也值得研究迭代器,特别是因为它被要求采用标准方法:
$arr = range(1, 3);
$it = new CachingIterator(new ArrayIterator($arr));
foreach($it as $key => $value)
{
if (!$it->hasNext()) echo 'Last:';
echo $value, "\n";
}
Run Code Online (Sandbox Code Playgroud)
您可能会发现某些内容对其他案例也更加灵活.
Rah*_*eel 13
一种方法是检测迭代器是否有next
.如果没有下一个附加到迭代器,则意味着您处于最后一个循环中.
foreach ($some_array as $element) {
if(!next($some_array)) {
// This is the last $element
}
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*der 10
从 foreach 数组中获取第一个和最后一个元素
foreach($array as $value) {
if ($value === reset($array)) {
echo 'FIRST ELEMENT!';
}
if ($value === end($array)) {
echo 'LAST ITEM!';
}
}
Run Code Online (Sandbox Code Playgroud)
因此,如果您的数组具有唯一的数组值,那么确定上一次迭代是微不足道的:
foreach($array as $element) {
if ($element === end($array))
echo 'LAST ELEMENT!';
}
Run Code Online (Sandbox Code Playgroud)
如你所见,如果最后一个元素在数组中只出现一次,则可以正常工作,否则会出现误报.在它不是,你必须比较键(肯定是唯一的).
foreach($array as $key => $element) {
end($array);
if ($key === key($array))
echo 'LAST ELEMENT!';
}
Run Code Online (Sandbox Code Playgroud)
还要注意严格的coparision运算符,这在这种情况下非常重要.
不要在最后一个值后添加逗号:
数组:
$data = ['lorem', 'ipsum', 'dolor', 'sit', 'amet'];
Run Code Online (Sandbox Code Playgroud)
功能:
$result = "";
foreach($data as $value) {
$resut .= (next($data)) ? "$value, " : $value;
}
Run Code Online (Sandbox Code Playgroud)
结果:
print $result;
Run Code Online (Sandbox Code Playgroud)
lorem、ipsum、dolor、sit、amet
从 PHP 7.3 到现在您可以使用数组的最后一个键的值array_key_last($array)
并将其与当前键进行比较:
$last_key = array_key_last($array);
foreach ($array as $key => $value) {
if ($key == $last_key) {
// last element
} else {
// not last element
}
}
Run Code Online (Sandbox Code Playgroud)
您仍然可以将该方法与关联数组一起使用:
$keys = array_keys($array);
for ($i = 0, $l = count($array); $i < $l; ++$i) {
$key = $array[$i];
$value = $array[$key];
$isLastItem = ($i == ($l - 1));
// do stuff
}
// or this way...
$i = 0;
$l = count($array);
foreach ($array as $key => $value) {
$isLastItem = ($i == ($l - 1));
// do stuff
++$i;
}
Run Code Online (Sandbox Code Playgroud)
小智 5
假设您将数组存储在变量中...
foreach($array as $key=>$value)
{
echo $value;
if($key != count($array)-1) { echo ", "; }
}
Run Code Online (Sandbox Code Playgroud)