将csv文件隐藏到PHP二维数组中 [英] Coverting a csv-file to a PHP 2D-array

查看:185
本文介绍了将csv文件隐藏到PHP二维数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP新手,一直在努力将CSV文件读取到2D数组中。我使用以下文件'csv / team.csv':

  ID,昵称,Shirtnumber,位置
1, Jimmy,0,RightBack
2,Mark,3,CentreBack
3,Bryan,17,左中场
4,詹姆斯,23,前锋
5,安德烈,69,守门员$我希望能够:

    >
  1. 在html / css网页上显示各个
    玩家的'name','shirtnumber','position'等。

  2. 使用PHP函数 prev() current() next()来在玩家之间导航。
  3. 使用关联关键字(除了索引关键字)。

我的第一段代码如下所示:

  $ teamdata = file(csv / team.csv); 
foreach($ teamdata as $ playerline){
$ player = explode(,,$ playerline);
列表($ ID,$昵称,$ Shirtnumber,$位置)= $ player;
{
print_r($ player);回声< br>;
print_r(prev($ player));回声< br>;

HTML中的结果是:

<$ p阵列([0] => 5 [1] => Andre [2] => 69 [3] =>守门员)

这看起来很酷。然而,


  1. 为什么 list()不会为数组添加关联关键字?

  2. 为什么 prev()不起作用? (PHP没有给出任何错误信息或警告)

通过引入 $ myplayer ,我创建了一个数组 $ player

  $ teamdata = file(csv / team.csv); 
foreach($ teamdata as $ playerline){
$ player = explode(,,$ playerline);
列表($ ID,$昵称,$ Shirtnumber,$位置)= $ player;
$ myplayer [] = $ player;
}

print_r($ player);回声< br>;回声< br>;
print_r($ myplayer);回声< br>;回声< br>;

echo $ player [1];回声< br>;
echo $ player [3];回声< br>;回声< br>;
echo $ myplayer [1] [1];回声< br>;
echo $ myplayer [2] [1];回声< br>;

输出如下所示:

<$ p $ ([0] => 5 [1] => Andre [2] => 69 [3] =>守门员)

阵列0] =>阵列([0] => ID [1] =>昵称[2] => Shirtnumber [3] =>位置)
[1] =& ] => 1 [1] =>> Jimmy [2] => 0 [3] => RightBack)
[2] => Array([0] => 2 [1] = > Mark [2] => 3 [3] => CentreBack)
[3] => Array([0] => 3 [1] => Bryan [2] =>阵列([0] => 4 [1] => James [2] => 23 [3] => Striker)
[5] =>阵列([0] => 5 [1] => Andre [2] => 69 [3] =>守门员))

安德烈
守门员

吉米
马克

似乎几乎是我所需要的。然而。我问自己,这是否是正确的做法,因为:


  1. 函数 list() code>不能使用

  2. 函数 prev()不能使用

  3. 选择正确的'player-attribute'的编码是'复杂的',并且很容易发生错误。


    我有编程COBOL(lol)和Pascal的背景,但PHP(和Java)对我来说是全新的。任何建议都会受到欢迎!

    解决方案


    1. list 只是将数组值赋给变量。

    您可能想要首先提取标题并将其与每行结合以获得关联array:

      $ teamdata = file(csv / team.csv,FILE_IGNORE_NEW_LINES); 
    //获取并删除第一行用作键
    $ headings = str_getcsv(array_shift($ teamdata));

    foreach($ teamdata as $ playerline){
    $ player = str_getcsv($ playerline);
    //将键与值相结合
    $ result [] = array_combine($ headings,$ player);
    {




    1. 您可以拨打 prev ,但是数组指针已经在第一个元素中,所以没有前一个元素,它返回 false 。这可以通过: var_dump(prev($ result));


    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,RightBack
    2,Mark,3,CentreBack
    3,Bryan,17,LeftMidfielder
    4,James,23,Striker
    5,Andre,69,Goalkeeper
    

    I would like to be able to:

    1. display the 'name', 'shirtnumber', 'position' etc. of individual players on a html/css-webpage.
    2. use the PHP-functions prev(), current(), next() to navigate between players.
    3. use associative keys (besides indexed keys).

    My first piece of code looked like this:

        $teamdata = file("csv/team.csv");
        foreach ($teamdata as $playerline) {
                $player = explode(",", $playerline);
                list($ID,$Nickname,$Shirtnumber,$Position) = $player;
        { 
        print_r($player); echo "<br>";
        print_r(prev($player)); echo "<br>";
    

    The result in HTML was:

    Array ( [0] => 5 [1] => Andre [2] => 69 [3] => Goalkeeper )
    

    This looks pretty cool. However,

    1. Why does list() not add associative keys to the array?
    2. Why does prev() not work? (PHP didn't give any error-message or warning)

    By introducing $myplayer, I created an array of $player.

        $teamdata = file("csv/team.csv");
        foreach ($teamdata as $playerline) {
                $player = explode(",", $playerline);
                list($ID,$Nickname,$Shirtnumber,$Position) =$player;
            $myplayer[]=$player;
    }   
    
        print_r($player); echo "<br>"; echo "<br>";
        print_r($myplayer); echo "<br>";echo "<br>";
    
        echo $player[1]; echo "<br>";
        echo $player[3]; echo "<br>"; echo "<br>";
        echo $myplayer[1][1]; echo "<br>";
        echo $myplayer[2][1]; echo "<br>";
    

    The output looked like:

    Array ( [0] => 5 [1] => Andre [2] => 69 [3] => Goalkeeper ) 
    
    Array ( [0] => Array ( [0] => ID [1] => Nickname [2] => Shirtnumber [3] => Position ) 
            [1] => Array ( [0] => 1 [1] => Jimmy [2] => 0 [3] => RightBack ) 
            [2] => Array ( [0] => 2 [1] => Mark [2] => 3 [3] => CentreBack ) 
            [3] => Array ( [0] => 3 [1] => Bryan [2] => 17 [3] => LeftMidfielder ) 
            [4] => Array ( [0] => 4 [1] => James [2] => 23 [3] => Striker ) 
            [5] => Array ( [0] => 5 [1] => Andre [2] => 69 [3] => Goalkeeper ) ) 
    
    Andre
    Goalkeeper
    
    Jimmy
    Mark
    

    This seems pretty much what I needed. However. I ask myself if this is the right way to do it, because:

    1. The function list() can't be used
    2. The function prev() can't be used
    3. Coding for selecting the correct 'player-attribute' is 'complex' and errors can easily be made

    I have a background in programming COBOL (lol) and Pascal, but PHP (and Java) is totally new for me. Any advise would be welcome!

    解决方案

    1. list just assigns array values to variables.

    You probably want to extract the headings first and combine that with each row to get an associative array:

    $teamdata = file("csv/team.csv", FILE_IGNORE_NEW_LINES);
    //get and remove first line to use as keys
    $headings = str_getcsv(array_shift($teamdata));
    
    foreach ($teamdata as $playerline) {
            $player = str_getcsv($playerline);
            //combine keys with values
            $result[] = array_combine($headings, $player);
    { 
    

    1. You call prev on the array, however the array pointer is already at the first element so there is no previous and it returns false. This is seen with: var_dump(prev($result));

    这篇关于将csv文件隐藏到PHP二维数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆