2

I got this simple example to illustrate what issue am having.

Suppose you have:

$myArray =  array("A","B","C","D","E","F","I","G","H");

The goal is to remove first elements of this Array in a loop.

Let's say:

for($i=0; $i<count($myArray ); $i++){

    var_dump($myArray );

//...Remove the first Element of this array while $i is less than it's length.

    array_shift($myArray); 
}

This Removes the first element in the first loop and two or three more, suddenly, it gives up removing the first Elements.

As per the Documentation:

array_shift() shifts the first value of the array off and returns it, shortening the array by one element and moving everything down. All numerical array keys will be modified to start counting from zero while literal keys won't be touched. And the it suggests this example.

   <?php 
         $stack = array("orange", "banana", "apple", "raspberry"); 
         $fruit = array_shift($stack); 
print_r($stack); 
?>

The Out put: Array ( [0] => banana [1] => apple [2] => raspberry )

The Example clearly shows what I want. But, why doesn't it continue removing to the Last element?

If this is how it was designed to be. Then is there anyway in to achieve the Removal/Deletion of first Array Element to the Last Element... Until count($myArray) returns 0 (Zero).??

Any Suggestion is Highly appreciated.

2
  • youre calling count every loop, it dwindles down thats why it doesnt finish. it goes only up to 4. if you want to finish and shift everything, use while instead Commented Jun 17, 2014 at 6:17
  • Thx... So, how do you loop through??... I tried assigning that number to a variable to avoid passing it to the for loop but Nothing Worked.... Commented Jun 17, 2014 at 6:19

4 Answers 4

5

Simply:

while (count($myArray) > 0) {
    array_shift($myArray); 
}

See Demo

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

3 Comments

while(!empty($myArray)) { is also ok
@ErickBest Take a look at this. Notice that $i is getting bigger as count($myArray) is getting smaller... when they meet in the middle, the loop ends.
+++1... lol, I never thought of that. :) (Accepted you answer!!)
2

The problem with your for loop is that. Every iteration you are calling count(). Therefore that number will dwindle down and the loop will only meet halfway (since your shifting it) and it will end halfway. If you really want to use a for loop, you need to initialize it count first. Consider this example:

for($x = 0, $size = count($myArray); $x < $size; $x++) {
    var_dump($myArray);
    array_shift($myArray);
}

// or the preferred one

while(!empty($myArray)) {
    var_dump($myArray);
    array_shift($myArray);
}

2 Comments

+1 Thanks a million for clearing up that Logic... that again UpVotes @Mark M's answer... the while loop way.
@ErickBest yes i also upvoted his answer. your welcome
1
while (count($myArray) > 0) {
    array_shift($myArray); 
}

That should do the trick but wouldn't it be more efficient to use :

unset($foo);
$foo = array();

(If you want to delete all the data ?).

1 Comment

I don't intend to suddenly clear up the array... I aimed at removing elements sequentially.. thx though.
1

Here is the dump of your code:

array (size=9)
  0 => string 'A' (length=1)
  1 => string 'B' (length=1)
  2 => string 'C' (length=1)
  3 => string 'D' (length=1)
  4 => string 'E' (length=1)
  5 => string 'F' (length=1)
  6 => string 'I' (length=1)
  7 => string 'G' (length=1)
  8 => string 'H' (length=1)
array (size=8)
  0 => string 'B' (length=1)
  1 => string 'C' (length=1)
  2 => string 'D' (length=1)
  3 => string 'E' (length=1)
  4 => string 'F' (length=1)
  5 => string 'I' (length=1)
  6 => string 'G' (length=1)
  7 => string 'H' (length=1)
array (size=7)
  0 => string 'C' (length=1)
  1 => string 'D' (length=1)
  2 => string 'E' (length=1)
  3 => string 'F' (length=1)
  4 => string 'I' (length=1)
  5 => string 'G' (length=1)
  6 => string 'H' (length=1)
array (size=6)
  0 => string 'D' (length=1)
  1 => string 'E' (length=1)
  2 => string 'F' (length=1)
  3 => string 'I' (length=1)
  4 => string 'G' (length=1)
  5 => string 'H' (length=1)
array (size=5)
  0 => string 'E' (length=1)
  1 => string 'F' (length=1)
  2 => string 'I' (length=1)
  3 => string 'G' (length=1)
  4 => string 'H' (length=1)

The 6th iteration is expected to work on the 6th elements of the array. However the length of the array is 5 at this moment. So nothing gonna happen. This explains the problem you have.

If you do want to go through the array, you'd better use while count(..).

while (count($myArray) > 0) {
    var_dump($myArray);
    array_shift($myArray); 
}

Generally speaking, it is NOT a good practice to change an array in a for-loop. In other languages, array might be read-only in a for-loop.

1 Comment

So how to go through all loops??... any Idea?

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.