Anonymous 发表于 2023-5-22 11:10:24

foreach循环中最后一个循环的值

foreach循环中最后一个循环的值
原始代码:
<?php
$i = count($myArray);
foreach ($myArray as $item) {
    /** code goes here... */
    $i--;
    if ($i == 0) {
      /** something happens here */
    }
}
?>



最后一个值
if ($item === end($myArray)) {
    // Last item
}第一个值
if ($item === reset($myArray)) {
    // First item
}

页: [1]
查看完整版本: foreach循环中最后一个循环的值