Results 1 to 3 of 3

Thread: Trying to auto increment a foreach loop

  1. #1
    Join Date
    Dec 2013
    Melbourne, Australia

    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;
            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;
    "Line (one ): " $line;
    $testing next explode(PHP_EOL$this->attachments[$this->filename]) );
    "Line (next): " $testing;

  2. #2
    Join Date
    Aug 2004
    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)) {
    some_condition($line)) {
    $key$line) = each($theArray);
    // $line is now from the next element of the array

    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Dec 2013
    Melbourne, Australia
    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

    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