I am new to PHP and continue to read the CSV file into a 2D array. I am using the following csv / team.csv file:
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:
- display "name", "shirt number", "position", etc. individual players on the html / css page.
- use the PHP functions
prev() , current() , next() to move between players. - 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>";
Result in HTML:
Array ( [0] => 5 [1] => Andre [2] => 69 [3] => Goalkeeper )
It looks pretty cool. but
- Why doesn't
list() add associative keys to an array? - Why does
prev() not work? (PHP did not give any error message or warning)
By entering $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 result looked like this:
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 is similar to what I need. But. I ask myself if this is right, because:
list() function cannot be used- Cannot use
prev() function - The coding for choosing the right “player attribute” is “complex” and errors can be easily made.
I have programming experience with COBOL (lol) and Pascal, but PHP (and Java) is completely new to me. Any recommendations are welcome!