www.webdeveloper.com
Results 1 to 3 of 3

Thread: Trying to auto increment a foreach loop

  1. #1
    Join Date
    Dec 2013
    Location
    Melbourne, Australia
    Posts
    26

    Trying to auto increment a foreach loop

    I am processing a file line by line using a foreach loop.

    I have a heading line which is 2 lines in some places where the header has wrapped.

    What I am looking to do is just auto process the next line once I identify the heading line.

    Initially I set a boolean and when I hit the header line, I set it to true and then next loop, if (true) I processed the next line but it's a bit ugly...

    Is there a way to process the next line and continue? I've tried next() but it doesn't appear to get the next line or advance the counter...

    Here is what I am trying to do (but isn't working) - I'm just looking for a cleaner solution than setting a boolean and looping and setting again...

    Note: there are lots of if, else statements - this is the ony one I'm posting

    PHP Code:
    foreach( explode(PHP_EOL$this->attachments[$this->filename]) as $index => $line ) {

        if ( 
    trimsubstr($line0strlen($field_index[0])) ) == "Tech" ) {
                                
            
    //If the array is set, we don't need to process it again
            
    if (!empty($title_index) ) continue;
                    
            
    $field_length=0;
            foreach    (
    $field_index as $index => $field) {
                        
                
    $title_index[$index] = trimsubstr($line$field_lengthstrlen($field) ) );
                        
                
    //set startpos of next column
                
    $field_length $field_length+strlen($field)+2;
            }
            print 
    "Line (one ): " $line;
            
    $testing next explode(PHP_EOL$this->attachments[$this->filename]) );
            print 
    "Line (next): " $testing;
                    
        }



  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,314
    I'd probably read the file line by line with fgets() within a while() loop. Then at any point if you need to read the next line, just call fgets() again. This will also use less RAM, as it does not have to store the entire file into an array (not a big deal if it's a small file, for some undefined value of "small").

    If you prefer looping through an array for whatever reason, then instead of foreach(), you could use each() within a while() loop, making your additional calls to each() when you need the next line at a particular point:
    PHP Code:
    while(list($key$line) = each($theArray)) {
        if(
    some_condition($line)) {
            list(
    $key$line) = each($theArray);
            
    // $line is now from the next element of the array
        
    }

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2013
    Location
    Melbourne, Australia
    Posts
    26
    Thanks again,

    The problem is that it's not really a file in the true sense of the meaning, it's an email attachment part which has been extracted as a string using
    imap_fetchbody()
    .

    thanks for both options.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles