#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    44
    Rep Power
    6

    Changing Array Size Inside Foreach


    i have a foreach statement which looks like this
    PHP Code:
    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

    PHP Code:
    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
    PHP Code:
    for($iterator 0$iterator count(static::$_components); $iterator++){
    ...
    some code

    is even worse

    Thanks in advance
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,572
    Rep Power
    595
    Just off the top of my head I'm thinking you could use something like this at the end of your loop.
    PHP Code:
    if (!next(static::$_components// meaning loop is about to exit
       
    if (key(static::$_components) != end(static::$_components)) { // another element must have been added
          //do whatever
       
    }

    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,316
    Rep Power
    7170
    I don't believe there is any way to change that behavior of foreach.


    PHP Code:
    <?PHP
        $array 
    = array(123);
        
        
    $func = function(&$element) use (&$array) {
            echo 
    "Process {$element}\n";
            
            if(
    $element === 3) {
                
    $array[] = "{$element} added";
            }
        };
        
        echo 
    "<pre>\n";
        
    array_walk($array$func);
        
    var_export($array);
    Last edited by E-Oreo; October 7th, 2012 at 03:51 PM.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,572
    Rep Power
    595
    I was not suggesting changing behavior but rather detecting the last iteration and then checking if an element was added. If so then process it.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    44
    Rep Power
    6
    Originally Posted by gw1500se
    Just off the top of my head I'm thinking you could use something like this at the end of your loop.
    PHP Code:
    if (!next(static::$_components// meaning loop is about to exit
       
    if (key(static::$_components) != end(static::$_components)) { // another element must have been added
          //do whatever
       
    }

    using end(static::$_components) automatically moves the internal pointer of the array to its end.
    means the loop will run 2 times.

    the only function which does not change the internal pointer is current()
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Your cross-post to phpfreaks has a link for you to read.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo