6

In PHP, How do I do this:

$test = array(1,2,3,4);

$i=0;
While (!empty($test)) {

  //do something with $test[i];

  //remove  $test[$i]; 
  unset($test[$i]);

}

Thanks

2 Answers 2

12

There are a few ways to modify an array while iterating through it:

You can use array_pop or array_shift depending on the order that you want the elements in

while( $val = array_pop( $arr ) )
-or-
while( $val = array_shift( $arr ) )

This does have a catch in this form, it will end for any falsy value listed in the type comparison table, array(), false, "", 0, "0", and null values will all end the while loop

Typically this is mitigated with

while( ( $val = array_pop( $arr ) ) !== null )

The strict type comparison should be used as null == false and the other falsey values. Again the issue will be that it will stop the iteration if a value in the array is actually null

A safer way of iterating through an array is to test that it's not empty:

while( $arr )
//while( count( $arr ) )
//while( !empty( $arr ) )
{
  $val = array_pop( $arr );

  //for forward iteration
  //$val = array_shift( $arr );
}

It's really that simple empty() is the opposite of an if() check. There is a hidden advantage to !empty() in the while loop, which is that it suppresses errors if the variable is undefined at the time of the check.

In well-written code™ this isn't an issue, however well-written code™ doesn't occur naturally in the wild, so you should be extra careful just-in-case.

Sign up to request clarification or add additional context in comments.

1 Comment

Especially because well-written code rarely occurs naturally, it's a bad idea to preemptively write code in a way that will suppress error messages on incorrect usage. Instead, your development, staging and live environments should be set up to deal with error messages in the correct way.
7

Perhaps you mean:

$test = array(1,2,3,4);

while (!empty($test)) {
  //do something with $test[0];

  //remove $test[0]; 
  unset($test[0]);
}

which will effectively pop the head of the array until it's empty.

It might make more sense to use array_pop, which pulls the last element from the array:

while(!empty($test)){
    $value = array_pop($test);
    // do work with $value
}

...or array_shift which will pull from the front of the array:

while(!empty($test)){
    $value = array_shift($test);
    // do work with $value
}

4 Comments

this works great except the pointer starts at the end? ie, if it echo out the values it shows : 4321. I need it to go 1234. reverse the array first?
There are a number of issues with this code. What happens if your array contains 0, false, null, "" or array()? Things will break.
Using !== instead of != will narrow the problem to NULL values only. To get them to work too, the while condition must be changed to !empty($arr), and assigning $value should happen inside the while body edit... which is exactly what zzzzBov said. Haven't read his answer until after I wrote that
You would think that something as simple as array modification during iteration would be an easy, solved problem, but as is evident here it's riddled with issues that are easy to miss.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.