vintox Posted October 7, 2012 Share Posted October 7, 2012 (edited) 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 October 7, 2012 by vintox Quote Link to comment Share on other sites More sharing options...
Andy123 Posted October 7, 2012 Share Posted October 7, 2012 (edited) 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 October 7, 2012 by Andy123 Quote Link to comment Share on other sites More sharing options...
vintox Posted October 7, 2012 Author Share Posted October 7, 2012 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. Quote Link to comment Share on other sites More sharing options...
Christian F. Posted October 7, 2012 Share Posted October 7, 2012 I recommend that you read the PHP manual on foreach (), as there is a warning that is very much relevant for your issue. Quote Link to comment Share on other sites More sharing options...
derwert Posted October 7, 2012 Share Posted October 7, 2012 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() Quote Link to comment Share on other sites More sharing options...
xyph Posted October 8, 2012 Share Posted October 8, 2012 Can you please give us a small snippet of code to recreate your issue. You are being vague and using some confusing language to try and explain your problem. Quote Link to comment Share on other sites More sharing options...
ManiacDan Posted October 8, 2012 Share Posted October 8, 2012 You may be interested in this article I wrote about exactly what happens when you do this. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.