Coverting a csv-file to a PHP 2D-array

前端 未结 2 1451
隐瞒了意图╮
隐瞒了意图╮ 2021-01-18 19:20

I\'m new at PHP and keep struggling to read a CSV file into a 2D-array. I use the following file \'csv/team.csv\':

ID,Nickname,Shirtnumber,Position
1,Jimmy,0         


        
2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-18 20:06

    I think str_getcsv comment contains very laconic solution, although it can be improved by using array_walk $userdata parameter:

    array_walk($teamdata, function(&$player, $_, $headers) {
        $player = array_combine($headers, $player);
    }, array_shift($teamdata));
    

    Here is the demo.

提交回复
热议问题