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

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    222
    Rep Power
    96

    Looping through array and changing a column of the previous row


    Here's what I need to do:

    1. Loop through $DaystarDCIImport array.
    2. If the 'PartNo' column is not a duplicate of a previous row then add that row to $WooCoomm.
    3. If the 'PartNo' column is a duplicate of a previous row then take the data in 'tax:vehicle_make' column and append it to the data in the same column of the last row in $WooComm.

    My problem being how to track the offset of rows between $DaystarDCIImport and $WooComm.

    The data is coming from a CSV file so I'm able to control the rows so that any duplicate rows will be right after each other. There will never be a situation where a duplicate row found in $DaystarDCIImport will be anywhere but the LAST row in $WooCoomm.

    PHP Code:
    $dupecount 1;
    for (
    $i 0$ilen count($DaystarDCIImport), $i $ilen$i++) {
        
    // check if current SKU is a duplicate of previous row
        
    if ($i AND $DaystarDCIImport[$i]['PartNo'] == $DaystarDCIImport[$i-$dupecount]['PartNo'] AND $DaystarDCIImport[$i]['VehicleMake'] != "All" AND $DaystarDCIImport[$i]['VehicleYearStart'] != "All"){
            
    $vehicle_make $DaystarDCIImport[$i]['VehicleMake'];
            if (!empty(
    $DaystarDCIImport[$i]['VehicleModel'])) {$vehicle_make .= " > ".$DaystarDCIImport[$i]['VehicleModel']." > ".$DaystarDCIImport[$i]['VehicleModel'];}
            if (!empty(
    $DaystarDCIImport[$i]['VehicleYearStart'] AND $DaystarDCIImport[$i]['VehicleYearStart'] != "All")) {$vehicle_make .= " ".$DaystarDCIImport[$i]['VehicleYearStart']."-".$DaystarDCIImport[$i]['VehicleYearEnd'];}
            else {
    $vehicle_make .= " All Years";}
            
    $WooComm[$i-$dupecount]['tax:vehicle_make'] .= " | ".$vehicle_make;
            
    $dupecount += 1;
            echo 
    " Count is: ".$i." DupeCount is: ".$dupecount."<br />";
    //        echo "Count is: ".$i."<br />";
    //        echo "DCI: ".$DaystarDCIImport[$i]['PartNo']."<br />";
    //        echo "<pre>";print_r($WooComm);echo "</pre>";echo "<br /><br />";
        
    }
        else {
            
    // Product Kit
            
    $product_kit $DaystarDCIImport[$i]['Category'];
            if (!empty(
    $DaystarDCIImport[$i]['SubCategory'])) {$product_kit .= " > ".$DaystarDCIImport[$i]['SubCategory'];}
            
    //if (!empty($DaystarDCIImport[$i]['3SubCategory'])) {$product_kit .= " > ".$DaystarDCIImport[$i]['3SubCategory'];}

            // Vehicle Make
            
    if (!empty($DaystarDCIImport[$i]['VehicleMake']) AND ($DaystarDCIImport[$i]['VehicleMake'] == "Universal" OR $DaystarDCIImport[$i]['VehicleMake'] == "All")) {$vehicle_make "Universal";}
            else {
                
    $vehicle_make $DaystarDCIImport[$i]['VehicleMake'];
                if (!empty(
    $DaystarDCIImport[$i]['VehicleModel'])) {$vehicle_make .= " > ".$DaystarDCIImport[$i]['VehicleModel']." > ".$DaystarDCIImport[$i]['VehicleModel'];}
                if (!empty(
    $DaystarDCIImport[$i]['VehicleYearStart'] && $DaystarDCIImport[$i]['VehicleYearStart'] != "All")) {$vehicle_make .= " ".$DaystarDCIImport[$i]['VehicleYearStart']."-".$DaystarDCIImport[$i]['VehicleYearEnd'];}
                else {
    $vehicle_make .= " All Years";}
            }
            
    // Product Features
            
    $product_features $DaystarDCIImport[$i]['Feature1'];
            if (!empty(
    $DaystarDCIImport[$i]['Feature2... ...9'])) {$product_features .= " | ".$DaystarDCIImport[$i]['Feature9'];}

            
    // Product Fitment Table
            
    if (!empty($DaystarDCIImport[$i]['VehicleMake']) AND $DaystarDCIImport[$i]['VehicleMake'] == "Universal") {$product_fitment_table "Universal";}
            else {
    $product_fitment_table $DaystarDCIImport[$i]['VehicleYearStart']."-".$DaystarDCIImport[$i]['VehicleYearEnd']." ".$DaystarDCIImport[$i]['VehicleMake']." ".$DaystarDCIImport[$i]['VehicleModel'];}

            
    // Creat Array with Web Data
            
    $WooComm[] = array(........);
            echo 
    " Count is: ".$i." DupeCount is: ".$dupecount;
    //        $dupecount += 1;
        
    }

    I'm looping through an array using $i as the counter.
    $dupecount is my counter for how many duplicates I've found.
    First I'm checking that $i > 0 because I can't subtract anything from zero. That would result in looking for $array[-1] which won't fly.
    Let's say $i = 1. Then I'm checking that 'PartNo' in row 1 is equal to 'PartNo' in row 1-$dupecount or row 0. If not duplicate then go to the else which creates a row in $WooComm.
    If row 1 is the same as the previous row then set $WooComm[$i]['Vehicle_Make'] equal to itself plus a divider and the extra copy in $vehicle_make.
    Then it bumps up $dupecount because the next iteration of the loop will need to look at TWO rows back to see if it's still a duplicate.

    I'm seriously missing a part of the logic but I don't know where it is...

    A sample of the data:

    When the counter is: The result array will be on this row:
    $DaystarDCIImport[$i] = 0 / $WooComm 0 row
    $DaystarDCIImport[$i] = 1 / $WooComm 1 row
    $DaystarDCIImport[$i] = 2 / $WooComm 2 row
    $DaystarDCIImport[$i] = 3 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 4 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 5 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 6 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 7 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 8 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 9 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 10 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 11 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 12 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 13 / $WooComm 2 row (duplicate)
    $DaystarDCIImport[$i] = 14 / $WooComm 3 row
    $DaystarDCIImport[$i] = 15 / $WooComm 3 row (duplicate)
    $DaystarDCIImport[$i] = 16 / $WooComm 3 row (duplicate)
    $DaystarDCIImport[$i] = 17 / $WooComm 3 row (duplicate)
    $DaystarDCIImport[$i] = 18 / $WooComm 4 row
    $DaystarDCIImport[$i] = 19 / $WooComm 4 row (duplicate)
    $DaystarDCIImport[$i] = 20 / $WooComm 4 row (duplicate)
    $DaystarDCIImport[$i] = 21 / $WooComm 4 row (duplicate)
    $DaystarDCIImport[$i] = 22 / $WooComm 4 row (duplicate)
    $DaystarDCIImport[$i] = 19 / $WooComm 4 row (duplicate)

    I've finally got it working for the first duplicate rows but it falls apart after that. I'm probably doing it one of the many hard ways but I only know so much...

    My next attempt will be to manually count the rows in $WooComm and compare the row in $DaystarDCIImport against the last row of $WooComm instead of the previous row in the same array. That may have a better chance of working but I'll post this question now since I'm a moron and likely making it harder than it needs to be...

    Thanks,

    Mike
  2. #2
  3. Forgotten Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,211
    Rep Power
    9644
    Originally Posted by big0mike
    Then it bumps up $dupecount because the next iteration of the loop will need to look at TWO rows back to see if it's still a duplicate.
    Or does it? Say $i-1 was a duplicate of $i-2. You say it needs to compare $i with $i-2 but wouldn't it be the same if you compared $i with $i-1?

    Originally Posted by big0mike
    My problem being how to track the offset of rows between $DaystarDCIImport and $WooComm.
    Associative arrays and references can be really nice here:
    - PartNo is the unique value so use that as the key in an array you dedicate to tracking. This means you can use isset() to see if the key already exists and thus simplify your check for duplicates.
    - If you need to find a location in $WooComm so you can modify it, store a reference to that location in the array. A reference means it will be the exact same array as in $WooComm and not merely a copy of it.

    PHP Code:
    $tracking = [];
    foreach (
    $DaystarDCIImport as $import) {
        
    $key $import['PartNo'];
        if (isset(
    $tracking[$key]) && /* other conditions... */) {
            
    // duplicate
            /* merge data into $tracking[$key] directly */
        
    } else { 
            
    // new record
            /* stuff... */

            
    $WooComm[] = array(...);
            
    $tracking[$key] =& $WooComm[count($WooComm) - 1]; // set up $tracking with a reference
        
    }

  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2007
    Location
    Glendale AZ
    Posts
    222
    Rep Power
    96
    Originally Posted by requinix
    Or does it? Say $i-1 was a duplicate of $i-2. You say it needs to compare $i with $i-2 but wouldn't it be the same if you compared $i with $i-1?
    Yes, that was something I was contemplating but for no known reason I thought that comparing it to the original was "better" than comparing it to the duplicate of the original Go figure.

    Originally Posted by requinix
    Associative arrays and references can be really nice here:
    - PartNo is the unique value so use that as the key in an array you dedicate to tracking. This means you can use isset() to see if the key already exists and thus simplify your check for duplicates.
    - If you need to find a location in $WooComm so you can modify it, store a reference to that location in the array. A reference means it will be the exact same array as in $WooComm and not merely a copy of it.
    That's a great option that I hadn't even thought of...

    I wound up solving it last night before I left with the secondary counter for the $WooComm array:

    PHP Code:
    $WooComm = array();
    $WooCount 0;
    for (
    $i 0$ilen count($DaystarDCIImport), $i $ilen$i++) {
        
    // check if current SKU is a duplicate of previous row
        
    if ($i AND $DaystarDCIImport[$i]['PartNo'] == $WooComm[$WooCount 1]['sku'] AND $DaystarDCIImport[$i]['VehicleMake'] != "All" AND $DaystarDCIImport[$i]['VehicleYearStart'] != "All") {
            
    $vehicle_make $DaystarDCIImport[$i]['VehicleMake'];
            if (!empty(
    $DaystarDCIImport[$i]['VehicleModel'])) {$vehicle_make .= " > ".$DaystarDCIImport[$i]['VehicleModel']." > ".$DaystarDCIImport[$i]['VehicleModel'];}
            if (!empty(
    $DaystarDCIImport[$i]['VehicleYearStart'] AND $DaystarDCIImport[$i]['VehicleYearStart'] != "All")) {$vehicle_make .= " ".$DaystarDCIImport[$i]['VehicleYearStart']."-".$DaystarDCIImport[$i]['VehicleYearEnd'];}
            else {
    $vehicle_make .= "All Years";}
            
    $WooComm[$WooCount-1]['tax:vehicle_make'] .= " | ".$vehicle_make;
        }
        else {... 
    bunch of stuff...
    // Create Array with Web Data
            
    $WooComm[$WooCount] = array(... ...);
            
    $WooCount += 1;
        }

    Now the only thing left I gotta do is weed out duplicates in 'VehicleMake' as in this instance there are 'PartNo's that are duplicate because the part can be used on the front or rear suspension. In that case it fits the same vehicle so my "category" string would have the make and model duplicated unnecessarily.

    Thanks for insight...

IMN logo majestic logo threadwatch logo seochat tools logo