Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As there is no iterator in PHP, the only way to loop through an array without getting the length of the array is to use foreach loop.

Let 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 that is to use for 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 the $testing_array is not originally using number as index, so I have to do some data massage to use a for loop. Is there a way I can repeat a specific iteration in a foreach loop?

like image 213
cytsunny Avatar asked Mar 05 '18 05:03

cytsunny


1 Answers

Perhaps a do-while will work for you.

Untested 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 a single loop structure that destroys the input array as it iterates.

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

Or you can use your for loop by indexing $test_array with array_values() if you don't need the keys in your // 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.
    }
}

If you do need the keys down script, but you want to use for, you could store an indexed array of keys which would allow you to use $i to access the keys and maintain data synchronicity.


My final suggestion:

Use while (key($testing_array) !== null) {...} to move the pointer without destroying elements.

Code: (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\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
    }
}

Output:

one => 1
    one => 2
    one => 3
    (advance pointer)
two => 2
    two => 3
    (advance pointer)
three => 3
    (advance pointer)
four => 4
    (advance pointer)
like image 123
mickmackusa Avatar answered Sep 22 '22 02:09

mickmackusa