简体   繁体   中英

Convert CSV to Multidimensional Associative Array

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);

1) fgetcsv could split lines by ";" by itself:

So, you can replace your while loop opening operator to the folowing:

while (($cells = fgetcsv($f, 0, ";")) !== false) {

And you wouldn't have to split line into cells manually.

2) You need a 2D - array, with every item contains (v1,v2,v3), so you could access it with instruction like $result[abc][T1] = (v1,v2,v3)

So, let's create it, and fill with data:

$result = [];
while (($cells = fgetcsv($f, 0, ";")) !== false) {
    // You have name in your first cell, right?
    $name = $cells[0];
    // And your timeframe in second one
    $timeframe = $cells[1];
    // v1, v2 and v3 are in third, fourth and fifth cells:
    $v123 = [$cells[2], $cells[3], $cells[4]];
    // And now put it into result:
    $result[$name][$timeframe] = $v123;
}

Now, to print as you need it:

// Printing header:
foreach ($result as $name => $result_by_name)
{ 
    echo "Name  ";
    foreach ($result_by_name as $timeframe => $v123)
    {
         echo $timeframe . "   ";
    }
    //End of header
    echo "\n";
    //exiting loop, as we need only 1 header
}

foreach ($result as $name => $result_by_name)
{ 
    echo $name . "    ";
    foreach ($result_by_name as $timeframe => $v123)
    {
        echo join(", ", $v123) . " ";
    }
    echo "\n";
}

Leaving good formatiing up to you :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM