Jump to content

Changing Array Inside Foreach


vintox

Recommended Posts

i have a foreach statement which looks like this

foreach(static::$_components as &$field){
..some code
}

 

in "some code" i sometimes add more components to "static::$_components" member

 

if the insertion happens not at the final iteration it works, and the foreach detect & use the last insertion.

but if the insertion happens at the last iteration

the foreach statement breaks(ends) without detecting & using the last insertion

 

there is any workaround for this issue

beside using for loops and count(array).

i prefer using foreach due to performance demands

 

for($iterator = 0, $field = ''; null != ($field = empty(static::$_components[$iterator]) ? null : static::$_components[$iterator]); $iterator++){
...some code
}

consumes a lot of memory and proccessing time

 

using

for($iterator = 0; $iterator < count(static::$_components); $iterator++){
...some code
}

is even worse

 

Thanks in advance

Edited by vintox
Link to comment
Share on other sites

I don't see the problem with your last example. In other languages, foreach uses an internal counter, so it is basically just a matter of preference and convenience. In PHP I am not entirely sure what happens internally, but it uses the array's internal pointer and probably makes use of next(). I haven't exactly studied this, but I don't see the problem with your last example; it is a common way to loop and as long as you are just using your counter to look up an array, that operation is O(1) - or constant - in the Big O Notation. Maybe I am missing something here, but that is what I would say without studying the inner workings of PHP's foreach loop further.

Edited by Andy123
Link to comment
Share on other sites

Hi Andy123,

the last 2 examples works and do the job but the consume a lot of memory and processing time.

foreach is a better way to work with arrays.

but with foreach is like it checks if its the end of the array before it executes the code inside

 

I don't see the problem with your last example. In other languages, foreach uses an internal counter, so it is basically just a matter of preference and convenience. In PHP I am not entirely sure what happens internally, but it uses the array's internal pointer and probably makes use of next(). I haven't exactly studied this, but I don't see the problem with your last example; it is a common way to loop and as long as you are just using your counter to look up an array, that operation is O(1) - or constant - in the Big O Notation. Maybe I am missing something here, but that is what I would say without studying the inner workings of PHP's foreach loop further.

Link to comment
Share on other sites

You'll also find a warning in the PHP Manual http://no2.php.net/m...uctures.for.php regarding the performance issue with using count in a for loop

The above code can be slow, because the array size is fetched on every iteration. Since the size never changes, the loop be easily optimized by using an intermediate variable to store the size instead of repeatedly calling count()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.