Foreach 最后一项获取方法
Foreach last item gets methode
伙计们,我有一个对象数组,
我希望 foreach 循环中的最后一项先做其他事情,然后再做其他事情。
我该如何存档?
if(sizeof($testDup) > 3){
} else {
foreach ($testDup as $d) {
}
}
$test array(3)
432 => test_id -> 21
431 => test_id -> 21
435 => test_id -> 21
这将处理对象数组并对最后一个元素执行其他操作:
$data = '';
$arrayWithObjects = array(
(object)array('test1', 'test2'),
(object)array('test1', 'test2'),
(object)array('test1', 'test2'),
);
foreach ($arrayWithObjects as $object) {
// Can't get next in the array, so is last element
if (!next($arrayWithObjects)) {
// Process last element
$data .= $object->{1};
} else {
// Process all other elements
$data .= $object->{0};
}
}
var_dump($data); // "test1test1test2"
您可以将当前版本与 end()
进行比较:
class Test {
public function __construct(private string $name) {}
public function read(): string {
return sprintf('%s: hurray', $this->name);
}
public function readLast():string {
return sprintf('%s: am I last?', $this->name);
}
}
$array = [
new Test('first'),
new Test('second'),
new Test('third'),
new Test('fourth'),
];
foreach( $array as $object ){
if($object === end($array)) {
echo $object->readLast().PHP_EOL;
}else{
echo $object->read().PHP_EOL;
}
}
作为检查当前项目是否为最后一项(其他答案显示)的替代方法,您可以使用 array_slice() to get the start of the array to loop over and then end() 获取数组的最后一个元素。
$data = [/*...*/]
foreach ($item as array_splice($data, 0, -1, true) {
$item->foo();
}
if (($item = end($data) !== false) {
$item->bar();
}
在我看来,这段代码比嵌套的 if $item === end($data)
检查更容易阅读(并且像圈复杂度这样的指标也同意)。如果在您的具体情况下也是如此,则取决于循环中的确切内容以及其中有多少不同。
此外,如果您的阵列很大,这种方法可能会提供(稍微)更好的性能(但如果您的阵列很大并且性能差异很重要,请不要相信我的话 - 对两种解决方案进行基准测试读取数据)。
非常简单:当循环结束时,您仍然得到最后一个元素!!
if (!empty($arr)) {
foreach ($arr as $item) {
; // Do something with $item
}
// Here you still got last $item
echo var_export($item, true);
}
伙计们,我有一个对象数组, 我希望 foreach 循环中的最后一项先做其他事情,然后再做其他事情。 我该如何存档?
if(sizeof($testDup) > 3){
} else {
foreach ($testDup as $d) {
}
}
$test array(3)
432 => test_id -> 21
431 => test_id -> 21
435 => test_id -> 21
这将处理对象数组并对最后一个元素执行其他操作:
$data = '';
$arrayWithObjects = array(
(object)array('test1', 'test2'),
(object)array('test1', 'test2'),
(object)array('test1', 'test2'),
);
foreach ($arrayWithObjects as $object) {
// Can't get next in the array, so is last element
if (!next($arrayWithObjects)) {
// Process last element
$data .= $object->{1};
} else {
// Process all other elements
$data .= $object->{0};
}
}
var_dump($data); // "test1test1test2"
您可以将当前版本与 end()
进行比较:
class Test {
public function __construct(private string $name) {}
public function read(): string {
return sprintf('%s: hurray', $this->name);
}
public function readLast():string {
return sprintf('%s: am I last?', $this->name);
}
}
$array = [
new Test('first'),
new Test('second'),
new Test('third'),
new Test('fourth'),
];
foreach( $array as $object ){
if($object === end($array)) {
echo $object->readLast().PHP_EOL;
}else{
echo $object->read().PHP_EOL;
}
}
作为检查当前项目是否为最后一项(其他答案显示)的替代方法,您可以使用 array_slice() to get the start of the array to loop over and then end() 获取数组的最后一个元素。
$data = [/*...*/]
foreach ($item as array_splice($data, 0, -1, true) {
$item->foo();
}
if (($item = end($data) !== false) {
$item->bar();
}
在我看来,这段代码比嵌套的 if $item === end($data)
检查更容易阅读(并且像圈复杂度这样的指标也同意)。如果在您的具体情况下也是如此,则取决于循环中的确切内容以及其中有多少不同。
此外,如果您的阵列很大,这种方法可能会提供(稍微)更好的性能(但如果您的阵列很大并且性能差异很重要,请不要相信我的话 - 对两种解决方案进行基准测试读取数据)。
非常简单:当循环结束时,您仍然得到最后一个元素!!
if (!empty($arr)) {
foreach ($arr as $item) {
; // Do something with $item
}
// Here you still got last $item
echo var_export($item, true);
}