Skip to main content
off by one error ?
Source Link
Jens A. Koch
  • 42.1k
  • 14
  • 123
  • 147

You might try using foreach/unset, instead of array_shift. <?php // Task: get and remove the first element of the array

$array = array(0, 1, 2, 3, 4, 5);

foreach($array as $value)
{
    // with each pass get the value
    // use method to doSomethingWithValue($value);
    echo $value;
    // and then remove that from the array 
    unset($array[$value]);
}
//so at the end of 56 rounds the array will be empty
assert('empty($array) /* Array must be empty. */');
?>

You might try using foreach/unset, instead of array_shift. <?php // Task: get and remove the first element of the array

$array = array(0, 1, 2, 3, 4, 5);

foreach($array as $value)
{
    // with each pass get the value
    // use method to doSomethingWithValue($value);
    echo $value;
    // and then remove that from the array 
    unset($array[$value]);
}
//so at the end of 5 rounds the array will be empty
assert('empty($array) /* Array must be empty. */');
?>

You might try using foreach/unset, instead of array_shift. <?php // Task: get and remove the first element of the array

$array = array(0, 1, 2, 3, 4, 5);

foreach($array as $value)
{
    // with each pass get the value
    // use method to doSomethingWithValue($value);
    echo $value;
    // and then remove that from the array 
    unset($array[$value]);
}
//so at the end of 6 rounds the array will be empty
assert('empty($array) /* Array must be empty. */');
?>
Source Link
Jens A. Koch
  • 42.1k
  • 14
  • 123
  • 147

You might try using foreach/unset, instead of array_shift. <?php // Task: get and remove the first element of the array

$array = array(0, 1, 2, 3, 4, 5);

foreach($array as $value)
{
    // with each pass get the value
    // use method to doSomethingWithValue($value);
    echo $value;
    // and then remove that from the array 
    unset($array[$value]);
}
//so at the end of 5 rounds the array will be empty
assert('empty($array) /* Array must be empty. */');
?>