从CSV逻辑创建多维数组

时间:2016-10-05 05:35:01

标签: php arrays csv multidimensional-array fgetcsv

我想创建一个数组,它遵循我的" CSV逻辑"但是我很挣扎。

以下是 CSV 的内容示例:

$ awk -f script.awk file1 <(sort -k3.1nr file2)
name1 5.8
name2 3

所以,我尝试实现的是按 Level 创建维度。

T代表标题,ST代表 SubTitles

使用此示例,结果数组将为:

+------------+----------+-------------+---------+
|    Name    | Duration |    User    |  Level   |
+------------+----------+------------+----------+
| Bestall    | 7 Days   |            |    T     |
| Bestleft   | 6 Days   |            |   ST     |
| Turn       | 2 Days   |   Brother  |          |
| Jump       | 4 Days   |   Sister   |          |
| Bestdown   | 1 Day    |            |   ST     |
| Play       | 1 Day    |   Sister   |          |
+------------+----------+------------+----------+

我的实际代码(不工作)

[Bestall] => Array 
         (
            [Duration] => 7 Days
            [Bestleft] => Array 
                       (
                         [Duration] => 6 Days
                         [0] => Array 
                             (    
                                [Name] => Turn     
                                [Duration] => 2 Days
                                [User] => Brother
                             )
                         [1] => Array 
                             (    
                                [Name] => Jump
                                [Duration] => 4 Days
                                [User] => Sister
                             )
                       )
            [Bestdown] => Array
                       (                           
                         [Duration] => 1 Day
                         [0] => Array 
                             (    
                                [Name] => Play
                                [Duration] => 1 Day
                                [User] => Sister
                             )
                       )
         )

2 个答案:

答案 0 :(得分:0)

用于创建数组的CSV示例

$filename = base_url() . 'yor_file.csv';

$content = file_get_contents($filename);

$lines = explode("\n", $content);

$lenthOfCsvRows = 50;
$tempArray = array();
$csvArray = array();
for ($i = 1; $i <= $lenthOfCsvRows; $i++) {
    $data = explode("\t", $lines[$i]);

    $tempArray['nameKey'] = array(
        '1' => $data[0],
        '2' => $data[1],
        '3' => $data[2],
        '4' => $data[3],
    );
    $csvArray[]= $tempArray;
}
print_r($csvArray);

答案 1 :(得分:0)

以下是您需要的PHP代码:

.hover-DBMS110,
.hover-ITSP136 {
  position: absolute;
}