5

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!

5
  • What is the purpose of prev and next I'm not getting it. Commented Jan 31, 2018 at 19:24
  • Thank your for your quick reaction. I want to use functions such as prev(), current() and next() to make conditional statements. Commented Jan 31, 2018 at 19:36
  • If you can expand on that with pseudo code or flow/objective I can add it. Commented Jan 31, 2018 at 19:37
  • Im not this far yet really, I try to get my arrays on order first. However, I'm thinking about conditions such as: if (current($myplayer)==end($myplayer)) {reset($myplayer} Commented Jan 31, 2018 at 20:01
  • Okay, this works perfect for me, exactly what I was looking for. I can also use next() and prev() now. I didn't realize the pointer was at the first array element. Thanks a lot. Commented Feb 9, 2018 at 15:13

2 Answers 2

1
  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));
Sign up to request clarification or add additional context in comments.

1 Comment

So there was a PHP function to parse csv! Cannot underestimate PHP standard library... Comments on the doc page are inspiring, worth reading: php.net/str_getcsv
1

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.

1 Comment

Thanks, this works perfect aswell. Only a bit harder to understand for a newbie as myself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.