如何在PHP的foreach循环中重复特定的迭代?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在PHP的foreach循环中重复特定的迭代?相关的知识,希望对你有一定的参考价值。

由于php中没有迭代器,因此在不获取数组长度的情况下循环遍历数组的唯一方法是使用foreach循环。

假设我有以下循环:

foreach ($testing_array as $testing_entry) {
    $result = my_testing_api_call($testing_entry);
    if ($result == 'server dead')
        break;
    else if ($result == 'done') {
        // do something to handle success code
        continue;
    }
    else {
        sleep(5);
        // I want to retry my_testing_api_call with current $testing entry, but don't know what to write
    }
}

一种方法是使用for循环。

for ( $i=0; $i < count($testing_array); $i++ ) {
    $result = my_testing_api_call($testing_entry[$i]);
    if ($result == 'server dead')
        break;
    else if ($result == 'done') {
        // do something to handle success code
        continue;
    }
    else {
        sleep(5);
        $i--; //so it will repeat the current iteration.
    }
}

问题是$testing_array最初并不使用数字作为索引,所以我必须做一些数据按摩才能使用for循环。有没有办法在foreach循环中重复特定的迭代?

答案

也许一段时间会对你有用。

未经测试的代码:

foreach ($testing_array as $testing_entry) {
    do {
        $result = my_testing_api_call($testing_entry);
        if ($result == 'server dead') {
            break 2;  // break both loops
        } elseif ($result == 'done') {
            // do something to handle success code
        } else {
            sleep(5);
            // I want to retry my_testing_api_call with current $testing entry, but don't know what to write
        }
    } while ($result !== 'done');
}

或者是一个循环结构,它在迭代时破坏输入数组。

未经测试的代码:

$result = '';
while ($testing_array && $result !== 'server dead') {
    $result = my_testing_api_call(current($testing_array));
    if ($result == 'done') {
        // do something to handle success code
        array_shift($testing_array);
    } elseif ($result !== 'server dead') {
        sleep(5); // I want to retry my_testing_api_call with current $testing entry, but don't know what to write
    }
}

或者你可以使用你的for循环,通过$test_arrayarray_values()索引,如果你不需要你的// do something中的键。

$testing_array = array_values($testing_array);
for ($i=0, $count=count($testing_array); $i < $count; ++$i) {
    $result = my_testing_api_call($testing_entry[$i]);
    if ($result == 'server dead') {
        break;
    } else if ($result == 'done') {
        // do something to handle success code
    } else {
        sleep(5);
        --$i; //so it will repeat the current iteration.
    }
}

如果你确实需要按键向下脚本,但是你想使用for,你可以存储一个索引的键数组,这将允许你使用$i访问键并保持数据同步。


My final suggestion:

使用while (key($testing_array) !== null) {...}移动指针而不破坏元素。

代码:(Demo

$array1 = [
    "one" => 1,
    "two" => 2,
    "three" => 3,
    "four" => 4
];

while (key($array1)!==null) {  // check if the internal pointer points beyond the end of the elements list or the array is empty
    $current = current($array1);
    $key = key($array1);
    echo "$key => $current
";         // display current key value pair
    if ($current < 3) {                // an arbitrary condition
        echo "	";
        $array1[$key] = ++$current;    // increment the current value
    } else {                           // current value is satisfactory
        echo "	(advance pointer)
";
        next($array1);                 // move pointer
    }
}

输出:

one => 1
    one => 2
    one => 3
    (advance pointer)
two => 2
    two => 3
    (advance pointer)
three => 3
    (advance pointer)
four => 4
    (advance pointer)
另一答案

你试图在循环中处理两个不同的东西,这使得编写干净的控制流很困难。您可以将重试逻辑与结果处理分开:

function call_api_with_retry($entry) {
  do {
    if (is_defined($result)) sleep(5);
    $result = my_testing_api_call($entry);
  } while ($result != 'done' && $result != 'server dead');
  return $result;
}

foreach ($testing_array as $testing_entry) {
    $result = call_api_with_retry($testing_entry);
    if ($result == 'server dead')
        break;
    else if ($result == 'done') {
        // do something to handle success code
        continue;
    }
}

(可能存在语法错误,自编写PHP代码以来已经有一段时间了)

以上是关于如何在PHP的foreach循环中重复特定的迭代?的主要内容,如果未能解决你的问题,请参考以下文章

使用PHP与2个foreach循环中的数组进行比较时如何获取迭代次数

在每个 foreach 循环迭代中包含重复的刀片模板

如何在foreach循环中每5次迭代后定义html标签

在 foreach 循环中每次迭代后 PHP 回显

PHP foreach两次回送特定的数组项

perl foreach 循环跳过特定文件夹下特定文件的迭代