CSV to Array PHP Function

The function here makes it super easy to open up a CSV file and turn it into a PHP array. If you ever have to manipulate data in your applications, this can save a ton of time. I kept the function very simple, but you can obviously add on.

The best explanation is often an example, so we've included one here. But first, the function itself:

<?php

// CSV to Array Function
// Copyright (c) 2014-2015, Ink Plant
// https://inkplant.com/code/csv-to-array
// this version was last updated July 20, 2015

ini_set('auto_detect_line_endings',true);
function csv_to_array($file,$args=array()) {

	//key => default
	$fields = array(
		'header_row'=>true, //is there a row of headers before the actual data?
		'remove_header_row'=>true, //if there's a header row, this will remove it (after using it)
		'numeric_headers'=>false, //if true, ignore the names in the header row and use integers as array keys instead
		'trim_headers'=>true, //trim whitespace around header row values
		'trim_values'=>true, //trim whitespace around all non-header row values
		'debug'=>false, //set to true while testing if you run into troubles
		'lb'=>"\n", //line break character
	);
	foreach ($fields as $key => $default) {
		if (array_key_exists($key,$args)) { $$key = $args[$key]; }
		else { $$key = $default; }
	}

	if ($debug) { echo '<p>Opening '.htmlspecialchars($file).'&hellip;</p>'; }
	$data = array();

	$row = 0;
	if (($handle = fopen($file,'r')) !== false) {
		while (!feof($handle)) {
			$line = fgetcsv($handle, 10240);
			$num = count($line);
			$row++;
			if (($header_row) && ($row == 1)) { $data['headers'] = array(); }
			else { $data[$row] = array(); }
			for ($c=0; $c<$num; $c++) {
				$value = $line[$c];
				if (($header_row) && ($row == 1)) { //if this is part of the header row
					if ($trim_headers) { $value = trim($value); }
					if (in_array($value,$data['headers'])) { custom_die('There are duplicate values in the header row: '.htmlspecialchars($value).'.'); }
					else { $data['headers'][$c] = (string)$value; }
				} elseif (($header_row) && (!$numeric_headers)) { //if this isn't part of the header row, but there is a header row
					$key = $data['headers'][$c];
					if ($trim_values) { $value = trim($value); }
					$data[$row][$key] = $value;
				} else { //if there's not a header row at all
					if ($trim_values) { $value = trim($value); }
					$data[$row][$c] = $value;
				}
			}
		}
		fclose($handle);
		if ($remove_header_row) { unset($data['headers']); }
		if ($debug) { echo '<pre>'.print_r($data,true).'</pre>'; }
		return $data;
	} else {
		custom_die('There was an error opening the file.');
	}
}
?>

For our example, we grabbed 2013 population estimates from the U.S. Census Bureau and saved them as population.csv.

<?php

//set the location of your CSV file
$file = HOME_DIR.'inkplant.com/population.csv'; 

//turn that CSV file into an array
$data = csv_to_array($file,array('header_row'=>true));

//now print that array out onto the screen
echo '<pre>'.print_r($data,true).'</pre>';

?>

Here's that output:

<pre>Array
(
	[2] => Array
		(
			[SUMLEV] => 10
			[REGION] => 0
			[DIVISION] => 0
			[STATE] => 0
			[NAME] => United States
			[POPESTIMATE2013] => 316128839
			[POPEST18PLUS2013] => 242542967
			[PCNT_POPEST18PLUS] => 76.7
		)

	[3] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 6
			[STATE] => 1
			[NAME] => Alabama
			[POPESTIMATE2013] => 4833722
			[POPEST18PLUS2013] => 3722241
			[PCNT_POPEST18PLUS] => 77
		)

	[4] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 9
			[STATE] => 2
			[NAME] => Alaska
			[POPESTIMATE2013] => 735132
			[POPEST18PLUS2013] => 547000
			[PCNT_POPEST18PLUS] => 74.4
		)

	[5] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 4
			[NAME] => Arizona
			[POPESTIMATE2013] => 6626624
			[POPEST18PLUS2013] => 5009810
			[PCNT_POPEST18PLUS] => 75.6
		)

	[6] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 7
			[STATE] => 5
			[NAME] => Arkansas
			[POPESTIMATE2013] => 2959373
			[POPEST18PLUS2013] => 2249507
			[PCNT_POPEST18PLUS] => 76
		)

	[7] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 9
			[STATE] => 6
			[NAME] => California
			[POPESTIMATE2013] => 38332521
			[POPEST18PLUS2013] => 29157644
			[PCNT_POPEST18PLUS] => 76.1
		)

	[8] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 8
			[NAME] => Colorado
			[POPESTIMATE2013] => 5268367
			[POPEST18PLUS2013] => 4030435
			[PCNT_POPEST18PLUS] => 76.5
		)

	[9] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 1
			[STATE] => 9
			[NAME] => Connecticut
			[POPESTIMATE2013] => 3596080
			[POPEST18PLUS2013] => 2810514
			[PCNT_POPEST18PLUS] => 78.2
		)

	[10] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 10
			[NAME] => Delaware
			[POPESTIMATE2013] => 925749
			[POPEST18PLUS2013] => 722191
			[PCNT_POPEST18PLUS] => 78
		)

	[11] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 11
			[NAME] => District of Columbia
			[POPESTIMATE2013] => 646449
			[POPEST18PLUS2013] => 534975
			[PCNT_POPEST18PLUS] => 82.8
		)

	[12] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 12
			[NAME] => Florida
			[POPESTIMATE2013] => 19552860
			[POPEST18PLUS2013] => 15526186
			[PCNT_POPEST18PLUS] => 79.4
		)

	[13] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 13
			[NAME] => Georgia
			[POPESTIMATE2013] => 9992167
			[POPEST18PLUS2013] => 7502458
			[PCNT_POPEST18PLUS] => 75.1
		)

	[14] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 9
			[STATE] => 15
			[NAME] => Hawaii
			[POPESTIMATE2013] => 1404054
			[POPEST18PLUS2013] => 1096788
			[PCNT_POPEST18PLUS] => 78.1
		)

	[15] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 16
			[NAME] => Idaho
			[POPESTIMATE2013] => 1612136
			[POPEST18PLUS2013] => 1184355
			[PCNT_POPEST18PLUS] => 73.5
		)

	[16] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 3
			[STATE] => 17
			[NAME] => Illinois
			[POPESTIMATE2013] => 12882135
			[POPEST18PLUS2013] => 9858828
			[PCNT_POPEST18PLUS] => 76.5
		)

	[17] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 3
			[STATE] => 18
			[NAME] => Indiana
			[POPESTIMATE2013] => 6570902
			[POPEST18PLUS2013] => 4984875
			[PCNT_POPEST18PLUS] => 75.9
		)

	[18] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 4
			[STATE] => 19
			[NAME] => Iowa
			[POPESTIMATE2013] => 3090416
			[POPEST18PLUS2013] => 2366384
			[PCNT_POPEST18PLUS] => 76.6
		)

	[19] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 4
			[STATE] => 20
			[NAME] => Kansas
			[POPESTIMATE2013] => 2893957
			[POPEST18PLUS2013] => 2169865
			[PCNT_POPEST18PLUS] => 75
		)

	[20] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 6
			[STATE] => 21
			[NAME] => Kentucky
			[POPESTIMATE2013] => 4395295
			[POPEST18PLUS2013] => 3381291
			[PCNT_POPEST18PLUS] => 76.9
		)

	[21] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 7
			[STATE] => 22
			[NAME] => Louisiana
			[POPESTIMATE2013] => 4625470
			[POPEST18PLUS2013] => 3512513
			[PCNT_POPEST18PLUS] => 75.9
		)

	[22] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 1
			[STATE] => 23
			[NAME] => Maine
			[POPESTIMATE2013] => 1328302
			[POPEST18PLUS2013] => 1067026
			[PCNT_POPEST18PLUS] => 80.3
		)

	[23] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 24
			[NAME] => Maryland
			[POPESTIMATE2013] => 5928814
			[POPEST18PLUS2013] => 4584292
			[PCNT_POPEST18PLUS] => 77.3
		)

	[24] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 1
			[STATE] => 25
			[NAME] => Massachusetts
			[POPESTIMATE2013] => 6692824
			[POPEST18PLUS2013] => 5298878
			[PCNT_POPEST18PLUS] => 79.2
		)

	[25] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 3
			[STATE] => 26
			[NAME] => Michigan
			[POPESTIMATE2013] => 9895622
			[POPEST18PLUS2013] => 7650421
			[PCNT_POPEST18PLUS] => 77.3
		)

	[26] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 4
			[STATE] => 27
			[NAME] => Minnesota
			[POPESTIMATE2013] => 5420380
			[POPEST18PLUS2013] => 4141269
			[PCNT_POPEST18PLUS] => 76.4
		)

	[27] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 6
			[STATE] => 28
			[NAME] => Mississippi
			[POPESTIMATE2013] => 2991207
			[POPEST18PLUS2013] => 2253775
			[PCNT_POPEST18PLUS] => 75.3
		)

	[28] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 4
			[STATE] => 29
			[NAME] => Missouri
			[POPESTIMATE2013] => 6044171
			[POPEST18PLUS2013] => 4646486
			[PCNT_POPEST18PLUS] => 76.9
		)

	[29] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 30
			[NAME] => Montana
			[POPESTIMATE2013] => 1015165
			[POPEST18PLUS2013] => 791184
			[PCNT_POPEST18PLUS] => 77.9
		)

	[30] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 4
			[STATE] => 31
			[NAME] => Nebraska
			[POPESTIMATE2013] => 1868516
			[POPEST18PLUS2013] => 1404168
			[PCNT_POPEST18PLUS] => 75.1
		)

	[31] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 32
			[NAME] => Nevada
			[POPESTIMATE2013] => 2790136
			[POPEST18PLUS2013] => 2128531
			[PCNT_POPEST18PLUS] => 76.3
		)

	[32] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 1
			[STATE] => 33
			[NAME] => New Hampshire
			[POPESTIMATE2013] => 1323459
			[POPEST18PLUS2013] => 1052337
			[PCNT_POPEST18PLUS] => 79.5
		)

	[33] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 2
			[STATE] => 34
			[NAME] => New Jersey
			[POPESTIMATE2013] => 8899339
			[POPEST18PLUS2013] => 6877222
			[PCNT_POPEST18PLUS] => 77.3
		)

	[34] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 35
			[NAME] => New Mexico
			[POPESTIMATE2013] => 2085287
			[POPEST18PLUS2013] => 1577747
			[PCNT_POPEST18PLUS] => 75.7
		)

	[35] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 2
			[STATE] => 36
			[NAME] => New York
			[POPESTIMATE2013] => 19651127
			[POPEST18PLUS2013] => 15411151
			[PCNT_POPEST18PLUS] => 78.4
		)

	[36] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 37
			[NAME] => North Carolina
			[POPESTIMATE2013] => 9848060
			[POPEST18PLUS2013] => 7562455
			[PCNT_POPEST18PLUS] => 76.8
		)

	[37] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 4
			[STATE] => 38
			[NAME] => North Dakota
			[POPESTIMATE2013] => 723393
			[POPEST18PLUS2013] => 560705
			[PCNT_POPEST18PLUS] => 77.5
		)

	[38] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 3
			[STATE] => 39
			[NAME] => Ohio
			[POPESTIMATE2013] => 11570808
			[POPEST18PLUS2013] => 8920978
			[PCNT_POPEST18PLUS] => 77.1
		)

	[39] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 7
			[STATE] => 40
			[NAME] => Oklahoma
			[POPESTIMATE2013] => 3850568
			[POPEST18PLUS2013] => 2903541
			[PCNT_POPEST18PLUS] => 75.4
		)

	[40] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 9
			[STATE] => 41
			[NAME] => Oregon
			[POPESTIMATE2013] => 3930065
			[POPEST18PLUS2013] => 3072459
			[PCNT_POPEST18PLUS] => 78.2
		)

	[41] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 2
			[STATE] => 42
			[NAME] => Pennsylvania
			[POPESTIMATE2013] => 12773801
			[POPEST18PLUS2013] => 10058156
			[PCNT_POPEST18PLUS] => 78.7
		)

	[42] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 1
			[STATE] => 44
			[NAME] => Rhode Island
			[POPESTIMATE2013] => 1051511
			[POPEST18PLUS2013] => 837524
			[PCNT_POPEST18PLUS] => 79.6
		)

	[43] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 45
			[NAME] => South Carolina
			[POPESTIMATE2013] => 4774839
			[POPEST18PLUS2013] => 3695041
			[PCNT_POPEST18PLUS] => 77.4
		)

	[44] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 4
			[STATE] => 46
			[NAME] => South Dakota
			[POPESTIMATE2013] => 844877
			[POPEST18PLUS2013] => 636918
			[PCNT_POPEST18PLUS] => 75.4
		)

	[45] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 6
			[STATE] => 47
			[NAME] => Tennessee
			[POPESTIMATE2013] => 6495978
			[POPEST18PLUS2013] => 5004401
			[PCNT_POPEST18PLUS] => 77
		)

	[46] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 7
			[STATE] => 48
			[NAME] => Texas
			[POPESTIMATE2013] => 26448193
			[POPEST18PLUS2013] => 19406207
			[PCNT_POPEST18PLUS] => 73.4
		)

	[47] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 49
			[NAME] => Utah
			[POPESTIMATE2013] => 2900872
			[POPEST18PLUS2013] => 2004283
			[PCNT_POPEST18PLUS] => 69.1
		)

	[48] => Array
		(
			[SUMLEV] => 40
			[REGION] => 1
			[DIVISION] => 1
			[STATE] => 50
			[NAME] => Vermont
			[POPESTIMATE2013] => 626630
			[POPEST18PLUS2013] => 503929
			[PCNT_POPEST18PLUS] => 80.4
		)

	[49] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 51
			[NAME] => Virginia
			[POPESTIMATE2013] => 8260405
			[POPEST18PLUS2013] => 6395870
			[PCNT_POPEST18PLUS] => 77.4
		)

	[50] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 9
			[STATE] => 53
			[NAME] => Washington
			[POPESTIMATE2013] => 6971406
			[POPEST18PLUS2013] => 5375611
			[PCNT_POPEST18PLUS] => 77.1
		)

	[51] => Array
		(
			[SUMLEV] => 40
			[REGION] => 3
			[DIVISION] => 5
			[STATE] => 54
			[NAME] => West Virginia
			[POPESTIMATE2013] => 1854304
			[POPEST18PLUS2013] => 1472626
			[PCNT_POPEST18PLUS] => 79.4
		)

	[52] => Array
		(
			[SUMLEV] => 40
			[REGION] => 2
			[DIVISION] => 3
			[STATE] => 55
			[NAME] => Wisconsin
			[POPESTIMATE2013] => 5742713
			[POPEST18PLUS2013] => 4434937
			[PCNT_POPEST18PLUS] => 77.2
		)

	[53] => Array
		(
			[SUMLEV] => 40
			[REGION] => 4
			[DIVISION] => 8
			[STATE] => 56
			[NAME] => Wyoming
			[POPESTIMATE2013] => 582658
			[POPEST18PLUS2013] => 444979
			[PCNT_POPEST18PLUS] => 76.4
		)

	[54] => Array
		(
			[SUMLEV] => 40
			[REGION] => X
			[DIVISION] => X
			[STATE] => 72
			[NAME] => Puerto Rico Commonwealth
			[POPESTIMATE2013] => 3615086
			[POPEST18PLUS2013] => 2801018
			[PCNT_POPEST18PLUS] => 77.5
		)

	[55] => Array
		(
			[SUMLEV] => 
		)

)
</pre>

Now that you have the array, what's next? Try using the array to table function to display a nice HTML table or the array to chart function to generate a colorful chart. If you want, you could even do some math and then save as another CSV file with this array to CSV function.

If you need more flexibility than this function offers (like, say, the ability to open up a TSV file instead of a CSV file), check out our TSV File to Array function instead. Or, if you need to process a really big file that exceeds your memory allocation, try our CSV to MySQL Queries function that breaks up the CSV line-by-line into manageable MySQL query files that can be uploaded to phpMyAdmin.


Comments

Loading…

This post was first published on August 7th, 2014 and last updated on November 3rd, 2015 by Robert James Reese in PHP. Before using any of the code or other content in this post, you must read and agree to our terms of use.