How do I load a multi-dimensional array using a loop?
I'm reading comma-delimited data from a .csv file and want to place it into a memory array (will attempt putting in mySQL db later).
I am already using the data -- using the file() function to read each line into a one-dimension array and then using explode() to break each value / cell out.
Now I need to compare cells from different rows, and change some of them. I need to have all the data available, not one line at a time, so I'd like to put the values into a multi-dim array.
Every example I find for loading a multi-dimensional array shows line-by-line loading, none use a loop so that the array size can be determined by the input data file.
Example code that I'm currently using -- need to convert to multi-dim array:
<?php
$d_array = file("dinput.csv") ;
$number_lines = count($d_array) ;
// strip cr-lf
for ($i=0; $i<$number_lines; $i++ ) {
$d_array[$i] = rtrim($d_array[$i]) ;
}
// current code -- processes one line at a time
for ($i=0; $i<$number_lines; $i++ ) {
$cell = explode(',', $d_array[$i]) ;
// process stuff here ...
}
/*
-- I now need to read all data into multi-dim array,
so can process all lines together
-- how do I do that?
*/
?>
thanks