I have a csv file with the following structure
name;timeframe;value1;value2;value3
The sample file looks like this
Abc;T1;V1,V2,V3
Abc;T2;V1,V2,V3
Abc;T3;V1,V2,V3
Xyz;T1;V1,V2,V3
Xyz;T2;V1,V2,V3
Xyz;T3;V1,V2,V3
The names can be repeated with different time frames, and there can be 8 different time frames.
What I want to do is to read this csv file in PHP and convert this into an Assosicative Multidimensional array. So that I can print it as a table later on
Name T1 T2 T3
Abc (v1,v2,v3) (v1,v2,v3) (v1,v2,v3)
Xyz (v1,v2,v3) (v1,v2,v3) (v1,v2,v3)
Def (v1,v2,v3) (v1,v2,v3) (v1,v2,v3)
So far I can read the csv file and print the individual rows and split them into separate fields. What I am stuck at is to use the associative arrays to hold this information in such a way that I can access it like
print($my_data_table['Abc']['T1'][V1])
Not sure what is the best way to create such structure. The reason I am interested in associated array is that the data is appended in the data file so with a new record at the end of the file it will be much easier to just replace the values of the corresponding entry in the existing array based on the 'Name' field in the record. This is my code so far
$f = fopen("scanner.txt", "r");
$names_array = array();
while (($line = fgetcsv($f)) !== false) {
$row = $line[0];
$cells = explode(";",$row);
$names_array[$cells[0]] = array("T1","T2","T3");
foreach ($cells as $cell) {
//TODO: How to hold the values (v1,v2,v3) in specific cells of the data table
}
}
fclose($f);
$csv = array_map('str_getcsv', file('data.csv'));