How to convert a csv string to a multidimensional array in php

I often like to setup special debugging scripts as part of my development process. Sometimes I need to open up a spreadsheet and setup a table of values that I then use to pass to the scripts that execute some functions, spit out some output and I can make sure that no errors occur — call it LazyMan Testing Method.

I have created a php class that will convert a list of comma separated values to objects in php.

Some quick features include:

  • CSV to Multidimensional Associative Array
  • CSV to Array of Std Class Objects
  • CSV to Array of Custom Object

Take a look at example for converting a csv to a custom object:

CSV to Object Array in PHP

For converting a CSV to Multidimensional Associative Array it is a simple as:

$data   =   <<<NOWDOC 
ColumnOne,Column Two,Header_Three 
1,2,3 
4,5,6 
NOWDOC; 

$Csv = new \projectivemotion\Csv\Csv(); 

$Csv->setString($data);

$CsvObjects  =   $Csv->AsObjects();

assert($CsvObjects[0]->ColumnOne === '1');
assert($CsvObjects[1]->ColumnOne === '4');
assert($CsvObjects[1]->{'Column Two'} === '5');

Check it out on github!
https://github.com/projectivemotion/csv-to-objects