How can I repeat a specific iteration in a foreach loop in PHP?

Since there is no iterator in PHP, the only way to iterate over an array without getting the length of the array is to use a foreach loop.

Say I have the following loop:

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
    }
}

One way to do this is to use a loop instead.

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.
    }
}

The problem is that $testing_arrayit does not initially use the number as an index, so I need to do some data massage to use the for loop. Is there a way to repeat a specific iteration in a foreach loop?

+4
source share
2 answers

Perhaps for now you will be working.

Unverified code:

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');
}

Or one loop structure that destroys the input array when it repeats.

Unverified code:

$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_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.
    }
}

script, 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
    }
}

:

one => 1
    one => 2
    one => 3
    (advance pointer)
two => 2
    two => 3
    (advance pointer)
three => 3
    (advance pointer)
four => 4
    (advance pointer)
+5

, . :

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-)

0
source

Source: https://habr.com/ru/post/1694447/


All Articles