简体   繁体   English

将CSV转换为多维关联数组

[英]Convert CSV to Multidimensional Associative Array

I have a csv file with the following structure 我有一个具有以下结构的csv文件

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. 名称可以在不同的时间范围内重复,并且可以有8个不同的时间范围。

What I want to do is to read this csv file in PHP and convert this into an Assosicative Multidimensional array. 我想要做的是在PHP中读取此csv文件,并将其转换为Associicative多维数组。 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. 到目前为止,我可以读取csv文件并打印各个行,并将它们拆分为单独的字段。 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 ";" 1)fgetcsv可以用“;”分隔行 by itself: 通过它自己:

So, you can replace your while loop opening operator to the folowing: 因此,您可以将以下while循环打开运算符替换为以下内容:

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) 2)您需要一个2D数组,每个项目都包含(v1,v2,v3),因此您可以使用$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 :) 留给您很好的格式化:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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