在数组迭代期间检查,如果当前元素是最后一个元素

she*_*iel 71 php arrays

请帮我把这个伪代码翻译成真正的PHP代码:

 foreach ($arr as $k => $v)
    if ( THIS IS NOT THE LAST ELEMENT IN THE ARRAY)
        doSomething();
Run Code Online (Sandbox Code Playgroud)

编辑:数组可能有数字或字符串键

Ibr*_*mar 123

你可以用PHP的结尾()

$array = array('a' => 1,'b' => 2,'c' => 3);
$lastElement = end($array);
foreach($array as $k => $v) {
    echo $v . '<br/>';
    if($v == $lastElement) {
         // 'you can do something here as this condition states it just entered last element of an array'; 
    }
}
Run Code Online (Sandbox Code Playgroud)

UPDATE1

正如@Mijoja指出的那样,如果你在数组中多次使用相同的值,那么上面可能会有问题.下面是它的修复.

$array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 2);
//point to end of the array
end($array);
//fetch key of the last element of the array.
$lastElementKey = key($array);
//iterate the array
foreach($array as $k => $v) {
    if($k == $lastElementKey) {
        //during array iteration this condition states the last element.
    }
}
Run Code Online (Sandbox Code Playgroud)

UPDATE2

我发现@onteria_的解决方案比我回答的更好,因为它没有修改数组内部指针,我正在更新答案以匹配他的答案.

$array = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 2);
// Get array keys
$arrayKeys = array_keys($array);
// Fetch last array key
$lastArrayKey = array_pop($arrayKeys);
//iterate array
foreach($array as $k => $v) {
    if($k == $lastArrayKey) {
        //during array iteration this condition states the last element.
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢@onteria_

  • 如果在数组中多次使用相同的值,则会出现问题 (9认同)
  • 谢谢@Mijoja指出.更新了代码:) (2认同)
  • @IbrahimazharArmar刚刚找到了这个答案,在实现您的解决方案时,我注意到自 PHP 7.3 以来,现在有了函数 `array_key_last`,它也返回数组的最后一个键而不影响内部指针:https://secure.php.net /array_key_last。会进一步简化您的解决方案:) (2认同)

Ric*_*ant 21

这总是对我有用

foreach($array as $key => $value) {
   if (end(array_keys($array)) == $key)
       // Last key reached
}
Run Code Online (Sandbox Code Playgroud)

编辑30/04/15

$last_key = end(array_keys($array));
reset($array);

foreach($array as $key => $value) {
  if ( $key == $last_key)
      // Last key reached
}
Run Code Online (Sandbox Code Playgroud)

避免@Warren Sergent提到的E_STRICT警告

$array_keys = array_keys($array);
$last_key = end($array_keys);
Run Code Online (Sandbox Code Playgroud)

  • @Pierce给你一个提示:这个问题是在接受答案后3年半得到回答的,另外,如果你的数组中有1000个项目,你将调用“end”1000次和“array_keys”1000次。 (2认同)

ont*_*ia_ 12

$myarray = array(
  'test1' => 'foo',
  'test2' => 'bar',
  'test3' => 'baz',
  'test4' => 'waldo'
);

$myarray2 = array(
'foo',
'bar',
'baz',
'waldo'
);

// Get the last array_key
$last = array_pop(array_keys($myarray));
foreach($myarray as $key => $value) {
  if($key != $last) {
    echo "$key -> $value\n";
  }
}

// Get the last array_key
$last = array_pop(array_keys($myarray2));
foreach($myarray2 as $key => $value) {
  if($key != $last) {
    echo "$key -> $value\n";
  }
}
Run Code Online (Sandbox Code Playgroud)

由于对它array_pop创建的临时数组的工作array_keys根本不会修改原始数组.

$ php test.php
test1 -> foo
test2 -> bar
test3 -> baz
0 -> foo
1 -> bar
2 -> baz
Run Code Online (Sandbox Code Playgroud)

  • 这会在E_STRICT中生成一个通知:运行时通知:只应通过引用传递变量 (2认同)

小智 5

为什么不使用这种非常简单的方法:

$i = 0; //a counter to track which element we are at
foreach($array as $index => $value) {
    $i++;
    if( $i == sizeof($array) ){
        //we are at the last element of the array
    }
}
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

93250 次

最近记录:

6 年,1 月 前