HadesMOD Posted April 17, 2014 Share Posted April 17, 2014 I am trying to read a large number of products, from a CSV file. I am attempting to parse each line and produce item lists, each with a Make, Model and set of OEM numbers. Multiple models use the same set of OEM values. Here is a sample screenshot of my product spreadsheet. - http://prntscr.com/3aorwh Here is my code below. Underneath will be my CSV contents (only a sample) error_reporting(0); $file = fopen("new.csv","r"); $makeArray = array(); $make = ''; $model = 0; $specificModel = ''; while(! feof($file)) { $lineVal = fgetcsv($file, 0, '@'); // Adds in 'Make' to initial makeArray array if ( isset($lineVal[0]) && empty( $lineVal[1] ) && empty( $lineVal[2] ) ) { $make = $lineVal[0]; $makeArray[$make] = array(); } // Gets Model lists if ($lineVal[1] !== '') { $model++; $makeArray[$make][$model] = array(); $specificModel = explode(",", $lineVal[1]); foreach($specificModel as $t) { $makeArray[$make][$model][$t] = array(); } } $OEMs = explode(',', $lineVal[2]); foreach($makeArray[$make][$model] as $specific) { if ($specific !== '') { // This loop adds in the variables foreach($OEMs as $oem){ $makeArray[$make][$model][$t][$oem] = array(); } } } } // Echo results echo '<pre>'; var_dump($makeArray) . '<br />'; echo '</pre>'; fclose($file); Here is the CSV content: Brother@@ Brother TN12K Compatible Black Toner Cartridge@HL-4200 Series@TN12BK Brother TN12C Compatible Cyan Toner Cartridge@@TN12C Brother TN12M Compatible Magenta Toner Cartridge@@TN12M Brother TN12Y Compatible Yellow Toner Cartridge@@TN12Y Brother TN04K Compatible Black Toner Cartridge, For HL-2700,@HL-2700CN,MFC-9420CN@TN04BK Brother TN04C Compatible Cyan Toner Cartridge, For HL-2700,@@TN04C Brother TN04M Compatible Magenta Toner Cartridge@@TN04M Brother TN04Y Compatible Yellow Toner Cartridge@@TN04Y When I output my code, the following structure occurs. Please note at this point, the OEM values are grouped, and each model within the dark borders on the left, has to have ALL of the values on the right side within these borders. Output array(1) { ["Brother"]=> array(2) { [1]=> array(1) { ["HL-4200 Series"]=> array(4) { ["TN12BK"]=> array(0) { } ["TN12C"]=> array(0) { } ["TN12M"]=> array(0) { } ["TN12Y"]=> array(0) { } } } [2]=> array(2) { ["HL-2700CN"]=> array(0) { } [" MFC-9420CN"]=> array(4) { ["TN04BK"]=> array(0) { } ["TN04C"]=> array(0) { } ["TN04M"]=> array(0) { } ["TN04Y "]=> array(0) { } } } } } Any takers as to why HL-2700CN and MFC-9420CN (or any sections like this) do not ALL get assigned the respective grouped OEMs? Additional information: The format of the end result needs to look like: Brother HL-4200 Series TN12BK TN12C TN12M TN12Y HL-2700CN TN04BK TN04C TN04M TN04Y MFC-9420CN TN04BK TN04C TN04M TN04Y Thanks Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 17, 2014 Share Posted April 17, 2014 based on your screenshot, all you would do is detect when the the nth field, the list of models, is not empty, and use that detection to end a previous set of data and start the new set. if you post some believable csv data, corresponding to the screenshot, someone might help with code that would do this. Quote Link to comment Share on other sites More sharing options...
HadesMOD Posted April 17, 2014 Author Share Posted April 17, 2014 I'm confused by what you are referring to. The CSV data matches the screenshot. The screenshot is an .xlsx file, which was deliminited with an '@' symbol. The first few lines of this were what I included in the sample. I did not see any location where attachments were available, but I have a whole CSV to reference. Regarding the detection of the nth field, I am unsure where you are referring to using this. The issue with the excel sheets and how they are formatted, is a bunch of models (where my original screenshot arrow is pointing to) each contain multiple fields. I am unsure of where this is going wrong in my code, but I understand the logic. I am fairly new to PHP, so a little guidance would help here. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 17, 2014 Share Posted April 17, 2014 the reason i asked for better/more complete data is because it's not possible to solve programming problems unless all the rules have been defined. what result do want when there are multiple OEM parts, such as the last section in your screenshot with two OEM parts each. Quote Link to comment Share on other sites More sharing options...
HadesMOD Posted April 17, 2014 Author Share Posted April 17, 2014 I'm looking to have a 3D array, starting with the Make. In the screenshot, Brother is the make for each of these. In the 2nd dimension, the Model is used. Each model, becomes an associative array with each value listed on the right side. See screenshot: http://prntscr.com/3aqhyn So for example, the DCP-9040CN, 9045CDN, etc.. in the model box (2nd column from right) would be assigned these values in their array. TN115BK, TN115C, TN115M, TN115Y, DR110CL The current problem is that it seems that only the last value (9830CDW) is getting the values listed above assigned, while the rest do not. I want every model to get these values in their array. Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted April 17, 2014 Share Posted April 17, 2014 you didn't answer the specific question i asked. anyway, after making an assumption about that specific case, see this code - <?php $file = fopen("new.csv","r"); $makeArray = array(); while(! feof($file)) { $lineVal = fgetcsv($file, 0, '@'); // Adds in 'Make' to initial makeArray array if ( isset($lineVal[0]) && empty( $lineVal[1] ) && empty( $lineVal[2] ) ) { $make = $lineVal[0]; $makeArray[$make] = array(); continue; } // Gets Model lists - first/only line with a $lineVal starts a set of data and lists the model(s) if ($lineVal[1] !== '') { $names = explode(",", $lineVal[1]); foreach($names as $name) { $makeArray[$make][$name] = array(); } } $OEMs = explode(',', $lineVal[2]); // there can be multiple parts listed on any line foreach($names as $name){ foreach($OEMs as $oem){ $makeArray[$make][$name][] = $oem; } } } // Echo results echo '<pre>'; var_dump($makeArray) . '<br />'; echo '</pre>'; fclose($file); Quote Link to comment Share on other sites More sharing options...
Solution HadesMOD Posted April 17, 2014 Author Solution Share Posted April 17, 2014 Hello, This worked perfectly and I've found my issues out from the revised code. Thanks, and sorry I was confused as to what information you lacked, otherwise I would've provided for sure. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.