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

cyt*_*nny 5 php iteration 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
    }
}
Run Code Online (Sandbox Code Playgroud)

一种方法是使用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.
    }
}
Run Code Online (Sandbox Code Playgroud)

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

mic*_*usa 8

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

未经测试的代码:

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');
}
Run Code Online (Sandbox Code Playgroud)

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

未经测试的代码:

$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
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,你可以用你for的索引循环$test_array使用array_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.
    }
}
Run Code Online (Sandbox Code Playgroud)

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


我的最后建议是:

用于while (key($testing_array) !== null) {...}在不破坏元素的情况下移动指针.

代码:( 演示)

$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\n";         // display current key value pair
    if ($current < 3) {                // an arbitrary condition
        echo "\t";
        $array1[$key] = ++$current;    // increment the current value
    } else {                           // current value is satisfactory
        echo "\t(advance pointer)\n";
        next($array1);                 // move pointer
    }
}
Run Code Online (Sandbox Code Playgroud)

输出:

one => 1
    one => 2
    one => 3
    (advance pointer)
two => 2
    two => 3
    (advance pointer)
three => 3
    (advance pointer)
four => 4
    (advance pointer)
Run Code Online (Sandbox Code Playgroud)