Covering a csv file in a 2D PHP array

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!

+5
source share
2 answers
  • list simply assigns array values ​​to variables.

You probably want to extract the headers first and combine them 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 in the array, however the array pointer is already in the first element, so there is no previous one, and it returns false . This can be seen with: var_dump(prev($result));
+1
source

I think the str_getcsv comment contains a very concise solution , although it can be improved using the array_walk $userdata parameter:

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

Here is a demo .

+1
source

Source: https://habr.com/ru/post/1275082/


All Articles