PHP - Csv_to_array And Array_combine
I can't remember where I found this csv_to_array function but I've used it with no problem until just recently. function csv_to_array($filename, $delimiter=',', $enclosure='"', $escape = '\\') { if(!file_exists($filename) AND !is_readable($filename)) {echo "File does not exist or is not readable."; return false;} $header = null; $values = array(); $data = array(); $lines = file($filename); foreach($lines as $line) { $values = str_getcsv($line, $delimiter, $enclosure, $escape); $values = array_map('trim', $values); if(!$header) {$header = $values; var_dump($header); echo "<br><br>"; var_dump($values); echo "<br><br>"; } else $data[] = array_combine($header, $values); } return $data; } I am now getting the "array_combine(): Both parameters should have an equal number of elements" error but I can't figure out how rows of the same CSV are coming up with different numbers of columns. Several columns have a fair amount of text including commas and I've formatted those columns as TEXT so they get quotes around them and I've taken the CSV, renamed it as a TXT file, and imported it back into Excel and all rows seem to have the correct number of columns. If they didn't it would be obvious as the data in the columns bounce around. I noticed in file that double quotes in the TEXT columns are escaped with another double quote. In the function it says the escape is \ so I did a search and replace "" > \\" but that had no effect. The var_dump section spits out two rows of the array. I went to row 45 in the CSV and see nothing out of the ordinary. array(46) { [0]=> string(6) "PartNo" [1]=> string(7) "Company" [2]=> string(6) "Status" [3]=> string(10) "DateEdited" [4]=> string(8) "Comments" [5]=> string(6) "Photos" [6]=> string(11) "VehicleType" [7]=> string(4) "Make" [8]=> string(5) "Model" [9]=> string(9) "BodyStyle" [10]=> string(9) "Driveline" [11]=> string(10) "LiftHeight" [12]=> string(7) "MaxTire" [13]=> string(9) "YearStart" [14]=> string(7) "YearEnd" [15]=> string(9) "Trademark" [16]=> string(6) "Series" [17]=> string(8) "Category" [18]=> string(11) "SubCategory" [19]=> string(12) "3SubCategory" [20]=> string(18) "AssociatedComments" [21]=> string(8) "Max 2000" [22]=> string(11) "Application" [23]=> string(8) "Max 2000" [24]=> string(11) "Daystar App" [25]=> string(12) "ExtendedDesc" [26]=> string(7) "Max 240" [27]=> string(13) "MarketingDesc" [28]=> string(8) "Max 2000" [29]=> string(16) "FeaturesBenefits" [30]=> string(3) "MAP" [31]=> string(4) "MSRP" [32]=> string(14) "PriceEffective" [33]=> string(11) "DateUpdated" [34]=> string(3) "UPC" [35]=> string(9) "BoxLength" [36]=> string(8) "BoxWidth" [37]=> string(9) "BoxHeight" [38]=> string(9) "BoxWeight" [39]=> string(5) "Color" [40]=> string(7) "Country" [41]=> string(6) "Carded" [42]=> string(9) "ScheduleB" } array(46) { [0]=> string(6) "PartNo" [1]=> string(7) "Company" [2]=> string(6) "Status" [3]=> string(10) "DateEdited" [4]=> string(8) "Comments" [5]=> string(6) "Photos" [6]=> string(11) "VehicleType" [7]=> string(4) "Make" [8]=> string(5) "Model" [9]=> string(9) "BodyStyle" [10]=> string(9) "Driveline" [11]=> string(10) "LiftHeight" [12]=> string(7) "MaxTire" [13]=> string(9) "YearStart" [14]=> string(7) "YearEnd" [15]=> string(9) "Trademark" [16]=> string(6) "Series" [17]=> string(8) "Category" [18]=> string(11) "SubCategory" [19]=> string(12) "3SubCategory" [20]=> string(18) "AssociatedComments" [21]=> string(8) "Max 2000" [22]=> string(11) "Application" [23]=> string(8) "Max 2000" [24]=> string(11) "Daystar App" [25]=> string(12) "ExtendedDesc" [26]=> string(7) "Max 240" [27]=> string(13) "MarketingDesc" [28]=> string(8) "Max 2000" [29]=> string(16) "FeaturesBenefits" [30]=> string(3) "MAP" [31]=> string(4) "MSRP" [32]=> string(14) "PriceEffective" [33]=> string(11) "DateUpdated" [34]=> string(3) "UPC" [35]=> string(9) "BoxLength" [36]=> string(8) "BoxWidth" [37]=> string(9) "BoxHeight" [38]=> string(9) "BoxWeight" [39]=> string(5) "Color" [40]=> string(7) "Country" [41]=> string(6) "Carded" [42]=> string(9) "ScheduleB" } And, the whole thing seems to loop indefinitely. A get hundreds of rows of the array_combine error followed by hundreds of rows of "Trying to access array offset on value of type bool" followed by hundreds of rows of "undefined index". When I replace the CSV I'm working on with the CSV that worked in the past I don't get the array_combine error. The CSVs are similar but with different data. The only difference that I know of being that the NEW file has several columns with soft returns to created spacing between paragraphs. I supposed that could be the problem but I'd think Excel would throw a fit when I try to import that data. I'm at a loss as to how I can figure this out. Any thoughts? Thanks, Mike Similar TutorialsHi, Actually I am working on text annotation tool, for this I have downloaded from github (https://github.com/ag-sc/SANTO), after ran the code I am getting error while uploading file into tool like Warning*: array_combine(): Both parameters should have an equal number of elements in *C:\xampp\htdocs\SANTO\blocks\importpub.ui.inc.php* on line *141* *Fatal error*: Uncaught mysqli_sql_exception: Column 'Text' cannot be null in C:\xampp\htdocs\SANTO\blocks\importpub.ui.inc.php:145 Stack trace: #0 C:\xampp\htdocs\SANTO\blocks\importpub.ui.inc.php(145): mysqli_stmt->execute() #1 C:\xampp\htdocs\SANTO\index2.php(36): include('C:\\xampp\\htdocs...') #2 C:\xampp\htdocs\SANTO\index2.php(54): action_include('importpub.ui') #3 {main} thrown in *C:\xampp\htdocs\SANTO\blocks\importpub.ui.inc.php* on line *145* Please can anyone please help me how to fix this issue, actually they have given readme file I didn't get the solution. Thank you in advance. |