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

    Join Date
    Nov 2003
    Posts
    41
    Rep Power
    12

    How to append the next record on previous line.


    Code:
    <?php
    
    $states = array( array(111, 222, "Alabama"),
                      array(111, 222, "California"),
                      array(111, 222, "Delaware"),
                      array(111, 222, "Oregon"),
                      array(222, 777, "Wyoming"),
                      array(333, 444, "Washington"),
                      array(333, 444, "Nevada"),
                      array(333, 444, "Colorado"),
                      array(898, 101, "Hawaii"));
    
    foreach ($states as $line) {
            if (($line[0] == $prevline0 && $line[1] == $prevline1) || empty($prevline0)) {
            } else {
                    print "$prevline0 :: $prevline1 :: $prevline2\n";
    
                    unset($prevline0, $prevline1, $prevline2);
            }
    
    $prevline0 = $line[0];
    $prevline1 = $line[1];
    $prevline2 .= "$line[2] ";
    }
    # OUTPUT I WANT - if the first 2 fields are identical, then print the states on the same line. However when I run my script, the last line (Hawaii) is missing.
    111 :: 222 :: Alabama California Delaware Oregon
    222 :: 777 :: Wyoming
    333 :: 444 :: Washington Nevada Colorado
    898 :: 101 :: Hawaii <= This line is missing.

    Thanks for any help.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    720
    Rep Power
    7
    It looks rather close, but I would not be printing the $prevline# vars. I would be using the $line[#] vars. You make it run almost nothing the first time thru, but you can make it take car of your first item right away. Instead of setting items at the end for the sake of printing them next time thru, just assign them for comparison.


    EDIT: Also, if you want
    111 :: 222 :: Alabama California Delaware Oregon
    to appear as such, instead of
    111 :: 222 :: Alabama
    111 :: 222 :: California
    111 :: 222 :: Delaware
    111 :: 222 :: Oregon
    then you would need to print the "111 :: 222 ::" first, have it enter the state as " state" with the space before the state next, and apply a <br /> when the following $line[] doesn't match.
    Last edited by Triple_Nothing; January 8th, 2013 at 02:47 AM.

IMN logo majestic logo threadwatch logo seochat tools logo