douyingtai6662 2014-01-08 23:57
浏览 37
已采纳

将csv内容放入多维数组中

I have a csv files that contains the following lines

enter code here

23,cars,43 063
23,cars,17 306
23,houses,13 300
23,garage,13 094
23,hotels,10 025
22,cars,75 675
22,cars,40 403
22,houses,32 243
22,garage,30 649
22,hotels,29 734
21,hotels,29 734
22,hotels,29 734

And here is the code that i am using to parse the csv file

     function readCSV($csvFile){
$file_handle = fopen($csvFile, 'r');
while (!feof($file_handle) ) {
    $line_of_text[] = fgetcsv($file_handle, 1024);
}



fclose($file_handle);
return $line_of_text;




    }

When i display the contents of the $line_of_text; array, here is what i get

      Array
       (
      [0] => Array
      (
        [0] => 23
        [1] => cars
        [2] => 43 063
      )

[1] => Array
    (
        [0] => 23
        [1] => cars
        [2] => 17 306
    )

[2] => Array
    (
        [0] => 23
        [1] => houses
        [2] => 13 300
    )

.....

But this is not what i want, i would like my array to be displayed in this way

 array (
 23 => array(
    'cars' => 43063,
    'houses' => 17306,
    'shops' => 13300,
    'garages' => 13094,
    'hotels' => 10025
 ),
 22 => array(
    'cars' => 75675,
    'houses' => 40403,
    'shops' => 32243,
    'garages' => 30649,
    'hotels' => 29734,
    'test' => 29734
     )
    );

How do i acheive this ? Can you please help me ?

//UPDATES contents of new my array

 Array
 (
 [0] => Array
    (
        [0] => cars
        [1] => 00
        [2] => 0
    )

[1] => Array
    (
        [0] => cars
        [1] => 01
        [2] => 0
    )

[2] => Array
    (
        [0] => cars
        [1] => 02
        [2] => 0
    )


[3] => Array
    (
        [0] => cars
        [1] => 11
        [2] => 0
    )

[4] => Array
    (
        [0] => cars
        [1] => 23
        [2] => 4425
    )

[5] => Array
    (
        [0] => houses
        [1] => 00
        [2] => 198
    )

[6] => Array
    (
        [0] => houses
        [1] => 01
        [2] => 742
    )

[7] => Array
    (
        [0] => houses
        [1] => 02
        [2] => 168
    )
)
  • 写回答

2条回答 默认 最新

  • doushi3454 2014-01-09 00:04
    关注
    $fp = fopen($csvFile, 'r');
    $master = array();
    while( $line = fgetcsv( $fp ) ) {
        // 23,cars,43 063
        if( !isset( $master[$line[0]] ) )
            $master[$line[0]] = array();
    
        if( !isset( $master[$line[0]][$line[1]] ) )
            $master[$line[0]][$line[1]] = 0;
    
        $n = filter_var($line[2], FILTER_SANITIZE_NUMBER_INT);
        $master[$line[0]][$line[1]] += $n;
    }
    
    var_dump( $master );
    

    Didn't test, but should work. Let me know if you have any issues.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化