It's still a problem of correction-the error has been known, but I don't know how to change PHPcode & lt ;? PhpfunctiongetCSVdata ($ filename) {$ row1; the first line starts if ($ handlefopen ($ filename, & quot; r & quot ;))! False) {whi is an error correction problem-the error is known.
I don't know how to change it.
PHP code
$ V) {if ($ k = $ c) // corresponding field {$ data [$ v] = $ dataSrc [$ c] ;}}} if (! Empty ($ data) {$ dataRtn [] = $ data; unset ($ data) ;}$ row ++ ;}fclose ($ handle); return $ dataRtn ;}} function print_array ($ data) // error in this function {$ row = count ($ data); $ column = count ($ data [0]); echo $ row; echo $ column; echo"
"; For ($ I = 0; $ I <$ row; $ I ++) {echo"
"; For ($ j = 0; $ j <$ column; $ j ++) {echo"
"; Echo $ data [$ I] [$ j]; echo" | ";} Echo"
";} Echo"
";}$ AData = getCSVdata('test.csv '); // print_r ($ aData); print_array ($ aData);?>
This is because you want to read the relevant table information in a file similar to an excel file from a csv file and display it in the way of index number [$ I] [$ j.
But the problem is that $ j is not the index of the column in the real data table, because in my file, the names of each column are actually "salary 1" "salary 2" "salary 3 "...
Such a Chinese name. So the error "Undefined offset: 0 in C: \ inetpub \ wwwroot \ test \ fileRead. php on line 54" is displayed.
So how can I solve this problem: when I don't know the index key of a column in advance, how can I retrieve them by index number?
In actual functions, I need to query the database based on the column name, and insert the value of the corresponding column into the column of the corresponding database table name,
For example, if $ data [I] ["payroll 1"] is inserted into the "payroll 1" column of the data table, you must be able to extract this string "payroll 1, in addition, the corresponding array element must also be obtained separately.
------ Solution --------------------
Use foreach loop
You 'd better post $ aData with var_export ($ aData)
------ Solution --------------------
Traverse the array, commonly used
Foreach ($ arr AS $ k => $ v) // $ k is the subscript and $ v is the value.
{
}
Or you can use the while list to traverse the array.
------ Solution --------------------
Foreach ($ data [$ I] as $ k => $ v) // $ k is the column name.