Manipulating CSV Data into Arrays with PHP

While I was working on the DoomCRM project today, I began to think about how we are going to migrate our vast amounts of historic data into our new database, which has a wildly different schema. Well, instead of having people go through and upload everything through the forntend, I decided it would be much easier to have them output the files as a CSV, then have PHP parse it into a usable format. ¬†Well, here’s how I did it (with some easy to understand comments)!

<?php
/*
* Let's take the header and put it into a header variable
*/

$header = ["Player name","Player ID","Team","Season","Start","End"];
/*
* Let's place our csv data into a variable. In real-world practice, you'd likely use fopen to open the csv file, assign the first line to the header, and the rest to a different variable.
*/

$str = "
Rude,35,5,1,2014-07-02,2014-08-24
Capodecima,7,5,1,2014-07-02,2014-08-24
Intoxicate,24,5,1,2014-07-02,2014-08-24
Dranzer,16,5,1,2014-07-02,2014-07-14
Zakken,49,3,1,2014-07-02,2014-08-24
Yorick,48,3,1,2014-07-02,2014-08-24
Bless,50,3,1,2014-07-02,2014-07-07
Legion,27,3,1,2014-07-02,2014-08-24
Ralphis,31,2,1,2014-07-02,2014-08-24
Hex,20,2,1,2014-07-02,2014-08-24
Starrk,38,2,1,2014-07-02,2014-08-24
RottKing,34,2,1,2014-07-02,2014-08-24
DemonSphere,10,4,1,2014-07-02,2014-08-24
Reckless,32,4,1,2014-07-02,2014-08-24
Shane,37,4,1,2014-07-02,2014-08-24
Perrondon,30,4,1,2014-07-02,2014-08-24
Water,45,1,1,2014-07-02,2014-08-24
Tai,39,1,1,2014-07-02,2014-08-24
Pollution,51,1,1,2014-07-02,2014-08-24
Alt_Stab,2,1,1,2014-07-02,2014-08-24
Climhazzard,41,6,1,2014-07-02,2014-08-24
Wazzup,46,6,1,2014-07-02,2014-08-24
nub_hat,29,6,1,2014-07-02,2014-08-24
Psyych,52,6,1,2014-07-02,2014-07-07
IdeIdoom,22,3,1,2014-07-07,2014-08-24
TMD,40,6,1,2014-07-07,2014-08-24
Bless,50,5,1,2014-07-14,2014-08-24
";

/*
* This bit of code breaks apart $str into an array wherein $line[$x] will contain a single line from the csv. The explode command breaks $str when it sees PHP_EOL, or, in human terms, an end of line character. We then use array_filter to get rid of the blank array entries that the explode command creates. We will need the $count variable to assign a key to our outer array.
*/

$line = array_filter(explode(PHP_EOL,$str));
$count = 0;

/*
* Now we take each $line as value $a and break it up into array $b using the comma character
*/

foreach($line as $a) 
{
  $b=explode(",",$a);

  /*
  * So now we have the following..
  *
  * $b[0]=Player Name
  * $b[1]=Player ID
  * $b[2]=Team
  * $b[3]=Season
  * $b[4]=Start
  * $b[5]=End
  *
  * We can now create the loop to assign our values to our new array.
  * The loop below takes the array size of the header (in this case it's 6), and assigns our comma separated values, which are stored in array $b, to the $out array, with a key of the the header key value.
  */

  for($i=0;$i<sizeof($header);$i++)
  {
    $out[$count][$header[$i]] = $b[$i];
  }
  $count++;
}
print_r($out);
?>

Quesitons? Feel free to contact me on Twitter – @shark_rad


Share On... Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Share on LinkedIn

Posted in Tutorials

Leave a Reply