2

I have this code snippet:

var_dump(count($xml)); // returns 34 

for($i = 8; $i < count($xml); $i++){ 
    echo "unseting $i <br>";
    unset($xml[$i]);
}

var_dump($xml);

Result:

int 34

unseting 8 unseting 9 unseting 10 unseting 11 unseting 12 unseting 13 unseting 14 unseting 15 unseting 16 unseting 17 unseting 18 unseting 19 unseting 20

Why is this for breaked at $i = 20 ?

When i change for-loop for $i = 0 - it's still works anomaly. I get only number 0-16 - simply always only half. But when i comment unset line - then it's iterate over all values..

Where can be problem in unset? Why unset breaking my for at half?

3
  • 2
    Because you reduce the length of $xml in your loop Commented Jul 2, 2016 at 6:08
  • Oh well - it's work fine now! Many thanks! Commented Jul 2, 2016 at 6:11
  • You should accept an answer if you've satisfied by any answers below. It's often useful for future visitors that has similar problem to find which answer that solve the problem. i.sstatic.net/LkiIZ.png Commented Jul 2, 2016 at 6:24

3 Answers 3

3

You'r experiencing this because unset() destroys the specified variables.

So when you iterate over the loop and at the same time you're using unset() to remove the element from array, array size also decreases along with removing elements from array,

So execution goes like this,

unseting 8 with remaining array size: 34 
unseting 9 with remaining array size: 33 
unseting 10 with remaining array size: 32 
unseting 11 with remaining array size: 31 
unseting 12 with remaining array size: 30 
unseting 13 with remaining array size: 29 
unseting 14 with remaining array size: 28 
unseting 15 with remaining array size: 27 
unseting 16 with remaining array size: 26 
unseting 17 with remaining array size: 25 
unseting 18 with remaining array size: 24 
unseting 19 with remaining array size: 23 
unseting 20 with remaining array size: 22 

Demo: https://eval.in/599426

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

Comments

1

When you unset you're shifting the items in the array, and the array is shrinking as the loop is running. What use to be at index $i, is now at index $i - 1 essentially.

Try running the loop backwards, and using $i-- instead.

for($i = count($xml); $i > 7; $i--){ 
    echo "unseting $i <br>";
    unset($xml[$i]);
}

You can also set the length before the loop runs so it's not changing during the loop like this:

$count = count($xml);
for ($i = 8; $i < $count; $++) {
    ...
}

1 Comment

$max = count($xml); for($i = 8; $i < $max; $i++){ ... } Works fine! Many thanks! :-)
0

unset removes elements from your indexed array, making the count() result decrease, influencing the end-condition of your for loop.

If your intension is to remove array elements from a certain index onwards, then you can just use array_splice:

array_splice($xml, 8);

Comments

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.