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:

1.   <?php
2.   
3.   // CSV to Array Function
4.   // Copyright (c) 2014-2015, Ink Plant
5.   // https://inkplant.com/code/csv-to-array
6.   // this version was last updated July 20, 2015
7.   
8.   ini_set('auto_detect_line_endings',true);
9.   function csv_to_array($file,$args=array()) {
10.  
11.  	//key => default
12.  	$fields = array(
13.  		'header_row'=>true, //is there a row of headers before the actual data?
14.  		'remove_header_row'=>true, //if there's a header row, this will remove it (after using it)
15.  		'numeric_headers'=>false, //if true, ignore the names in the header row and use integers as array keys instead
16.  		'trim_headers'=>true, //trim whitespace around header row values
17.  		'trim_values'=>true, //trim whitespace around all non-header row values
18.  		'debug'=>false, //set to true while testing if you run into troubles
19.  		'lb'=>"\n", //line break character
20.  	);
21.  	foreach ($fields as $key => $default) {
22.  		if (array_key_exists($key,$args)) { $$key = $args[$key]; }
23.  		else { $$key = $default; }
24.  	}
25.  
26.  	if ($debug) { echo '<p>Opening '.htmlspecialchars($file).'&hellip;</p>'; }
27.  	$data = array();
28.  
29.  	$row = 0;
30.  	if (($handle = fopen($file,'r')) !== false) {
31.  		while (!feof($handle)) {
32.  			$line = fgetcsv($handle, 10240);
33.  			$num = count($line);
34.  			$row++;
35.  			if (($header_row) && ($row == 1)) { $data['headers'] = array(); }
36.  			else { $data[$row] = array(); }
37.  			for ($c=0; $c<$num; $c++) {
38.  				$value = $line[$c];
39.  				if (($header_row) && ($row == 1)) { //if this is part of the header row
40.  					if ($trim_headers) { $value = trim($value); }
41.  					if (in_array($value,$data['headers'])) { custom_die('There are duplicate values in the header row: '.htmlspecialchars($value).'.'); }
42.  					else { $data['headers'][$c] = (string)$value; }
43.  				} elseif (($header_row) && (!$numeric_headers)) { //if this isn't part of the header row, but there is a header row
44.  					$key = $data['headers'][$c];
45.  					if ($trim_values) { $value = trim($value); }
46.  					$data[$row][$key] = $value;
47.  				} else { //if there's not a header row at all
48.  					if ($trim_values) { $value = trim($value); }
49.  					$data[$row][$c] = $value;
50.  				}
51.  			}
52.  		}
53.  		fclose($handle);
54.  		if ($remove_header_row) { unset($data['headers']); }
55.  		if ($debug) { echo '<pre>'.print_r($data,true).'</pre>'; }
56.  		return $data;
57.  	} else {
58.  		custom_die('There was an error opening the file.');
59.  	}
60.  }
61.  ?>

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

1.   <?php
2.   
3.   //set the location of your CSV file
4.   $file = HOME_DIR.'inkplant.com/population.csv'; 
5.   
6.   //turn that CSV file into an array
7.   $data = csv_to_array($file,array('header_row'=>true));
8.   
9.   //now print that array out onto the screen
10.  echo '<pre>'.print_r($data,true).'</pre>';
11.  
12.  ?>

Here's that output:

1.   <pre>Array
2.   (
3.   	[2] => Array
4.   		(
5.   			[SUMLEV] => 10
6.   			[REGION] => 0
7.   			[DIVISION] => 0
8.   			[STATE] => 0
9.   			[NAME] => United States
10.  			[POPESTIMATE2013] => 316128839
11.  			[POPEST18PLUS2013] => 242542967
12.  			[PCNT_POPEST18PLUS] => 76.7
13.  		)
14.  
15.  	[3] => Array
16.  		(
17.  			[SUMLEV] => 40
18.  			[REGION] => 3
19.  			[DIVISION] => 6
20.  			[STATE] => 1
21.  			[NAME] => Alabama
22.  			[POPESTIMATE2013] => 4833722
23.  			[POPEST18PLUS2013] => 3722241
24.  			[PCNT_POPEST18PLUS] => 77
25.  		)
26.  
27.  	[4] => Array
28.  		(
29.  			[SUMLEV] => 40
30.  			[REGION] => 4
31.  			[DIVISION] => 9
32.  			[STATE] => 2
33.  			[NAME] => Alaska
34.  			[POPESTIMATE2013] => 735132
35.  			[POPEST18PLUS2013] => 547000
36.  			[PCNT_POPEST18PLUS] => 74.4
37.  		)
38.  
39.  	[5] => Array
40.  		(
41.  			[SUMLEV] => 40
42.  			[REGION] => 4
43.  			[DIVISION] => 8
44.  			[STATE] => 4
45.  			[NAME] => Arizona
46.  			[POPESTIMATE2013] => 6626624
47.  			[POPEST18PLUS2013] => 5009810
48.  			[PCNT_POPEST18PLUS] => 75.6
49.  		)
50.  
51.  	[6] => Array
52.  		(
53.  			[SUMLEV] => 40
54.  			[REGION] => 3
55.  			[DIVISION] => 7
56.  			[STATE] => 5
57.  			[NAME] => Arkansas
58.  			[POPESTIMATE2013] => 2959373
59.  			[POPEST18PLUS2013] => 2249507
60.  			[PCNT_POPEST18PLUS] => 76
61.  		)
62.  
63.  	[7] => Array
64.  		(
65.  			[SUMLEV] => 40
66.  			[REGION] => 4
67.  			[DIVISION] => 9
68.  			[STATE] => 6
69.  			[NAME] => California
70.  			[POPESTIMATE2013] => 38332521
71.  			[POPEST18PLUS2013] => 29157644
72.  			[PCNT_POPEST18PLUS] => 76.1
73.  		)
74.  
75.  	[8] => Array
76.  		(
77.  			[SUMLEV] => 40
78.  			[REGION] => 4
79.  			[DIVISION] => 8
80.  			[STATE] => 8
81.  			[NAME] => Colorado
82.  			[POPESTIMATE2013] => 5268367
83.  			[POPEST18PLUS2013] => 4030435
84.  			[PCNT_POPEST18PLUS] => 76.5
85.  		)
86.  
87.  	[9] => Array
88.  		(
89.  			[SUMLEV] => 40
90.  			[REGION] => 1
91.  			[DIVISION] => 1
92.  			[STATE] => 9
93.  			[NAME] => Connecticut
94.  			[POPESTIMATE2013] => 3596080
95.  			[POPEST18PLUS2013] => 2810514
96.  			[PCNT_POPEST18PLUS] => 78.2
97.  		)
98.  
99.  	[10] => Array
100. 		(
101. 			[SUMLEV] => 40
102. 			[REGION] => 3
103. 			[DIVISION] => 5
104. 			[STATE] => 10
105. 			[NAME] => Delaware
106. 			[POPESTIMATE2013] => 925749
107. 			[POPEST18PLUS2013] => 722191
108. 			[PCNT_POPEST18PLUS] => 78
109. 		)
110. 
111. 	[11] => Array
112. 		(
113. 			[SUMLEV] => 40
114. 			[REGION] => 3
115. 			[DIVISION] => 5
116. 			[STATE] => 11
117. 			[NAME] => District of Columbia
118. 			[POPESTIMATE2013] => 646449
119. 			[POPEST18PLUS2013] => 534975
120. 			[PCNT_POPEST18PLUS] => 82.8
121. 		)
122. 
123. 	[12] => Array
124. 		(
125. 			[SUMLEV] => 40
126. 			[REGION] => 3
127. 			[DIVISION] => 5
128. 			[STATE] => 12
129. 			[NAME] => Florida
130. 			[POPESTIMATE2013] => 19552860
131. 			[POPEST18PLUS2013] => 15526186
132. 			[PCNT_POPEST18PLUS] => 79.4
133. 		)
134. 
135. 	[13] => Array
136. 		(
137. 			[SUMLEV] => 40
138. 			[REGION] => 3
139. 			[DIVISION] => 5
140. 			[STATE] => 13
141. 			[NAME] => Georgia
142. 			[POPESTIMATE2013] => 9992167
143. 			[POPEST18PLUS2013] => 7502458
144. 			[PCNT_POPEST18PLUS] => 75.1
145. 		)
146. 
147. 	[14] => Array
148. 		(
149. 			[SUMLEV] => 40
150. 			[REGION] => 4
151. 			[DIVISION] => 9
152. 			[STATE] => 15
153. 			[NAME] => Hawaii
154. 			[POPESTIMATE2013] => 1404054
155. 			[POPEST18PLUS2013] => 1096788
156. 			[PCNT_POPEST18PLUS] => 78.1
157. 		)
158. 
159. 	[15] => Array
160. 		(
161. 			[SUMLEV] => 40
162. 			[REGION] => 4
163. 			[DIVISION] => 8
164. 			[STATE] => 16
165. 			[NAME] => Idaho
166. 			[POPESTIMATE2013] => 1612136
167. 			[POPEST18PLUS2013] => 1184355
168. 			[PCNT_POPEST18PLUS] => 73.5
169. 		)
170. 
171. 	[16] => Array
172. 		(
173. 			[SUMLEV] => 40
174. 			[REGION] => 2
175. 			[DIVISION] => 3
176. 			[STATE] => 17
177. 			[NAME] => Illinois
178. 			[POPESTIMATE2013] => 12882135
179. 			[POPEST18PLUS2013] => 9858828
180. 			[PCNT_POPEST18PLUS] => 76.5
181. 		)
182. 
183. 	[17] => Array
184. 		(
185. 			[SUMLEV] => 40
186. 			[REGION] => 2
187. 			[DIVISION] => 3
188. 			[STATE] => 18
189. 			[NAME] => Indiana
190. 			[POPESTIMATE2013] => 6570902
191. 			[POPEST18PLUS2013] => 4984875
192. 			[PCNT_POPEST18PLUS] => 75.9
193. 		)
194. 
195. 	[18] => Array
196. 		(
197. 			[SUMLEV] => 40
198. 			[REGION] => 2
199. 			[DIVISION] => 4
200. 			[STATE] => 19
201. 			[NAME] => Iowa
202. 			[POPESTIMATE2013] => 3090416
203. 			[POPEST18PLUS2013] => 2366384
204. 			[PCNT_POPEST18PLUS] => 76.6
205. 		)
206. 
207. 	[19] => Array
208. 		(
209. 			[SUMLEV] => 40
210. 			[REGION] => 2
211. 			[DIVISION] => 4
212. 			[STATE] => 20
213. 			[NAME] => Kansas
214. 			[POPESTIMATE2013] => 2893957
215. 			[POPEST18PLUS2013] => 2169865
216. 			[PCNT_POPEST18PLUS] => 75
217. 		)
218. 
219. 	[20] => Array
220. 		(
221. 			[SUMLEV] => 40
222. 			[REGION] => 3
223. 			[DIVISION] => 6
224. 			[STATE] => 21
225. 			[NAME] => Kentucky
226. 			[POPESTIMATE2013] => 4395295
227. 			[POPEST18PLUS2013] => 3381291
228. 			[PCNT_POPEST18PLUS] => 76.9
229. 		)
230. 
231. 	[21] => Array
232. 		(
233. 			[SUMLEV] => 40
234. 			[REGION] => 3
235. 			[DIVISION] => 7
236. 			[STATE] => 22
237. 			[NAME] => Louisiana
238. 			[POPESTIMATE2013] => 4625470
239. 			[POPEST18PLUS2013] => 3512513
240. 			[PCNT_POPEST18PLUS] => 75.9
241. 		)
242. 
243. 	[22] => Array
244. 		(
245. 			[SUMLEV] => 40
246. 			[REGION] => 1
247. 			[DIVISION] => 1
248. 			[STATE] => 23
249. 			[NAME] => Maine
250. 			[POPESTIMATE2013] => 1328302
251. 			[POPEST18PLUS2013] => 1067026
252. 			[PCNT_POPEST18PLUS] => 80.3
253. 		)
254. 
255. 	[23] => Array
256. 		(
257. 			[SUMLEV] => 40
258. 			[REGION] => 3
259. 			[DIVISION] => 5
260. 			[STATE] => 24
261. 			[NAME] => Maryland
262. 			[POPESTIMATE2013] => 5928814
263. 			[POPEST18PLUS2013] => 4584292
264. 			[PCNT_POPEST18PLUS] => 77.3
265. 		)
266. 
267. 	[24] => Array
268. 		(
269. 			[SUMLEV] => 40
270. 			[REGION] => 1
271. 			[DIVISION] => 1
272. 			[STATE] => 25
273. 			[NAME] => Massachusetts
274. 			[POPESTIMATE2013] => 6692824
275. 			[POPEST18PLUS2013] => 5298878
276. 			[PCNT_POPEST18PLUS] => 79.2
277. 		)
278. 
279. 	[25] => Array
280. 		(
281. 			[SUMLEV] => 40
282. 			[REGION] => 2
283. 			[DIVISION] => 3
284. 			[STATE] => 26
285. 			[NAME] => Michigan
286. 			[POPESTIMATE2013] => 9895622
287. 			[POPEST18PLUS2013] => 7650421
288. 			[PCNT_POPEST18PLUS] => 77.3
289. 		)
290. 
291. 	[26] => Array
292. 		(
293. 			[SUMLEV] => 40
294. 			[REGION] => 2
295. 			[DIVISION] => 4
296. 			[STATE] => 27
297. 			[NAME] => Minnesota
298. 			[POPESTIMATE2013] => 5420380
299. 			[POPEST18PLUS2013] => 4141269
300. 			[PCNT_POPEST18PLUS] => 76.4
301. 		)
302. 
303. 	[27] => Array
304. 		(
305. 			[SUMLEV] => 40
306. 			[REGION] => 3
307. 			[DIVISION] => 6
308. 			[STATE] => 28
309. 			[NAME] => Mississippi
310. 			[POPESTIMATE2013] => 2991207
311. 			[POPEST18PLUS2013] => 2253775
312. 			[PCNT_POPEST18PLUS] => 75.3
313. 		)
314. 
315. 	[28] => Array
316. 		(
317. 			[SUMLEV] => 40
318. 			[REGION] => 2
319. 			[DIVISION] => 4
320. 			[STATE] => 29
321. 			[NAME] => Missouri
322. 			[POPESTIMATE2013] => 6044171
323. 			[POPEST18PLUS2013] => 4646486
324. 			[PCNT_POPEST18PLUS] => 76.9
325. 		)
326. 
327. 	[29] => Array
328. 		(
329. 			[SUMLEV] => 40
330. 			[REGION] => 4
331. 			[DIVISION] => 8
332. 			[STATE] => 30
333. 			[NAME] => Montana
334. 			[POPESTIMATE2013] => 1015165
335. 			[POPEST18PLUS2013] => 791184
336. 			[PCNT_POPEST18PLUS] => 77.9
337. 		)
338. 
339. 	[30] => Array
340. 		(
341. 			[SUMLEV] => 40
342. 			[REGION] => 2
343. 			[DIVISION] => 4
344. 			[STATE] => 31
345. 			[NAME] => Nebraska
346. 			[POPESTIMATE2013] => 1868516
347. 			[POPEST18PLUS2013] => 1404168
348. 			[PCNT_POPEST18PLUS] => 75.1
349. 		)
350. 
351. 	[31] => Array
352. 		(
353. 			[SUMLEV] => 40
354. 			[REGION] => 4
355. 			[DIVISION] => 8
356. 			[STATE] => 32
357. 			[NAME] => Nevada
358. 			[POPESTIMATE2013] => 2790136
359. 			[POPEST18PLUS2013] => 2128531
360. 			[PCNT_POPEST18PLUS] => 76.3
361. 		)
362. 
363. 	[32] => Array
364. 		(
365. 			[SUMLEV] => 40
366. 			[REGION] => 1
367. 			[DIVISION] => 1
368. 			[STATE] => 33
369. 			[NAME] => New Hampshire
370. 			[POPESTIMATE2013] => 1323459
371. 			[POPEST18PLUS2013] => 1052337
372. 			[PCNT_POPEST18PLUS] => 79.5
373. 		)
374. 
375. 	[33] => Array
376. 		(
377. 			[SUMLEV] => 40
378. 			[REGION] => 1
379. 			[DIVISION] => 2
380. 			[STATE] => 34
381. 			[NAME] => New Jersey
382. 			[POPESTIMATE2013] => 8899339
383. 			[POPEST18PLUS2013] => 6877222
384. 			[PCNT_POPEST18PLUS] => 77.3
385. 		)
386. 
387. 	[34] => Array
388. 		(
389. 			[SUMLEV] => 40
390. 			[REGION] => 4
391. 			[DIVISION] => 8
392. 			[STATE] => 35
393. 			[NAME] => New Mexico
394. 			[POPESTIMATE2013] => 2085287
395. 			[POPEST18PLUS2013] => 1577747
396. 			[PCNT_POPEST18PLUS] => 75.7
397. 		)
398. 
399. 	[35] => Array
400. 		(
401. 			[SUMLEV] => 40
402. 			[REGION] => 1
403. 			[DIVISION] => 2
404. 			[STATE] => 36
405. 			[NAME] => New York
406. 			[POPESTIMATE2013] => 19651127
407. 			[POPEST18PLUS2013] => 15411151
408. 			[PCNT_POPEST18PLUS] => 78.4
409. 		)
410. 
411. 	[36] => Array
412. 		(
413. 			[SUMLEV] => 40
414. 			[REGION] => 3
415. 			[DIVISION] => 5
416. 			[STATE] => 37
417. 			[NAME] => North Carolina
418. 			[POPESTIMATE2013] => 9848060
419. 			[POPEST18PLUS2013] => 7562455
420. 			[PCNT_POPEST18PLUS] => 76.8
421. 		)
422. 
423. 	[37] => Array
424. 		(
425. 			[SUMLEV] => 40
426. 			[REGION] => 2
427. 			[DIVISION] => 4
428. 			[STATE] => 38
429. 			[NAME] => North Dakota
430. 			[POPESTIMATE2013] => 723393
431. 			[POPEST18PLUS2013] => 560705
432. 			[PCNT_POPEST18PLUS] => 77.5
433. 		)
434. 
435. 	[38] => Array
436. 		(
437. 			[SUMLEV] => 40
438. 			[REGION] => 2
439. 			[DIVISION] => 3
440. 			[STATE] => 39
441. 			[NAME] => Ohio
442. 			[POPESTIMATE2013] => 11570808
443. 			[POPEST18PLUS2013] => 8920978
444. 			[PCNT_POPEST18PLUS] => 77.1
445. 		)
446. 
447. 	[39] => Array
448. 		(
449. 			[SUMLEV] => 40
450. 			[REGION] => 3
451. 			[DIVISION] => 7
452. 			[STATE] => 40
453. 			[NAME] => Oklahoma
454. 			[POPESTIMATE2013] => 3850568
455. 			[POPEST18PLUS2013] => 2903541
456. 			[PCNT_POPEST18PLUS] => 75.4
457. 		)
458. 
459. 	[40] => Array
460. 		(
461. 			[SUMLEV] => 40
462. 			[REGION] => 4
463. 			[DIVISION] => 9
464. 			[STATE] => 41
465. 			[NAME] => Oregon
466. 			[POPESTIMATE2013] => 3930065
467. 			[POPEST18PLUS2013] => 3072459
468. 			[PCNT_POPEST18PLUS] => 78.2
469. 		)
470. 
471. 	[41] => Array
472. 		(
473. 			[SUMLEV] => 40
474. 			[REGION] => 1
475. 			[DIVISION] => 2
476. 			[STATE] => 42
477. 			[NAME] => Pennsylvania
478. 			[POPESTIMATE2013] => 12773801
479. 			[POPEST18PLUS2013] => 10058156
480. 			[PCNT_POPEST18PLUS] => 78.7
481. 		)
482. 
483. 	[42] => Array
484. 		(
485. 			[SUMLEV] => 40
486. 			[REGION] => 1
487. 			[DIVISION] => 1
488. 			[STATE] => 44
489. 			[NAME] => Rhode Island
490. 			[POPESTIMATE2013] => 1051511
491. 			[POPEST18PLUS2013] => 837524
492. 			[PCNT_POPEST18PLUS] => 79.6
493. 		)
494. 
495. 	[43] => Array
496. 		(
497. 			[SUMLEV] => 40
498. 			[REGION] => 3
499. 			[DIVISION] => 5
500. 			[STATE] => 45
501. 			[NAME] => South Carolina
502. 			[POPESTIMATE2013] => 4774839
503. 			[POPEST18PLUS2013] => 3695041
504. 			[PCNT_POPEST18PLUS] => 77.4
505. 		)
506. 
507. 	[44] => Array
508. 		(
509. 			[SUMLEV] => 40
510. 			[REGION] => 2
511. 			[DIVISION] => 4
512. 			[STATE] => 46
513. 			[NAME] => South Dakota
514. 			[POPESTIMATE2013] => 844877
515. 			[POPEST18PLUS2013] => 636918
516. 			[PCNT_POPEST18PLUS] => 75.4
517. 		)
518. 
519. 	[45] => Array
520. 		(
521. 			[SUMLEV] => 40
522. 			[REGION] => 3
523. 			[DIVISION] => 6
524. 			[STATE] => 47
525. 			[NAME] => Tennessee
526. 			[POPESTIMATE2013] => 6495978
527. 			[POPEST18PLUS2013] => 5004401
528. 			[PCNT_POPEST18PLUS] => 77
529. 		)
530. 
531. 	[46] => Array
532. 		(
533. 			[SUMLEV] => 40
534. 			[REGION] => 3
535. 			[DIVISION] => 7
536. 			[STATE] => 48
537. 			[NAME] => Texas
538. 			[POPESTIMATE2013] => 26448193
539. 			[POPEST18PLUS2013] => 19406207
540. 			[PCNT_POPEST18PLUS] => 73.4
541. 		)
542. 
543. 	[47] => Array
544. 		(
545. 			[SUMLEV] => 40
546. 			[REGION] => 4
547. 			[DIVISION] => 8
548. 			[STATE] => 49
549. 			[NAME] => Utah
550. 			[POPESTIMATE2013] => 2900872
551. 			[POPEST18PLUS2013] => 2004283
552. 			[PCNT_POPEST18PLUS] => 69.1
553. 		)
554. 
555. 	[48] => Array
556. 		(
557. 			[SUMLEV] => 40
558. 			[REGION] => 1
559. 			[DIVISION] => 1
560. 			[STATE] => 50
561. 			[NAME] => Vermont
562. 			[POPESTIMATE2013] => 626630
563. 			[POPEST18PLUS2013] => 503929
564. 			[PCNT_POPEST18PLUS] => 80.4
565. 		)
566. 
567. 	[49] => Array
568. 		(
569. 			[SUMLEV] => 40
570. 			[REGION] => 3
571. 			[DIVISION] => 5
572. 			[STATE] => 51
573. 			[NAME] => Virginia
574. 			[POPESTIMATE2013] => 8260405
575. 			[POPEST18PLUS2013] => 6395870
576. 			[PCNT_POPEST18PLUS] => 77.4
577. 		)
578. 
579. 	[50] => Array
580. 		(
581. 			[SUMLEV] => 40
582. 			[REGION] => 4
583. 			[DIVISION] => 9
584. 			[STATE] => 53
585. 			[NAME] => Washington
586. 			[POPESTIMATE2013] => 6971406
587. 			[POPEST18PLUS2013] => 5375611
588. 			[PCNT_POPEST18PLUS] => 77.1
589. 		)
590. 
591. 	[51] => Array
592. 		(
593. 			[SUMLEV] => 40
594. 			[REGION] => 3
595. 			[DIVISION] => 5
596. 			[STATE] => 54
597. 			[NAME] => West Virginia
598. 			[POPESTIMATE2013] => 1854304
599. 			[POPEST18PLUS2013] => 1472626
600. 			[PCNT_POPEST18PLUS] => 79.4
601. 		)
602. 
603. 	[52] => Array
604. 		(
605. 			[SUMLEV] => 40
606. 			[REGION] => 2
607. 			[DIVISION] => 3
608. 			[STATE] => 55
609. 			[NAME] => Wisconsin
610. 			[POPESTIMATE2013] => 5742713
611. 			[POPEST18PLUS2013] => 4434937
612. 			[PCNT_POPEST18PLUS] => 77.2
613. 		)
614. 
615. 	[53] => Array
616. 		(
617. 			[SUMLEV] => 40
618. 			[REGION] => 4
619. 			[DIVISION] => 8
620. 			[STATE] => 56
621. 			[NAME] => Wyoming
622. 			[POPESTIMATE2013] => 582658
623. 			[POPEST18PLUS2013] => 444979
624. 			[PCNT_POPEST18PLUS] => 76.4
625. 		)
626. 
627. 	[54] => Array
628. 		(
629. 			[SUMLEV] => 40
630. 			[REGION] => X
631. 			[DIVISION] => X
632. 			[STATE] => 72
633. 			[NAME] => Puerto Rico Commonwealth
634. 			[POPESTIMATE2013] => 3615086
635. 			[POPEST18PLUS2013] => 2801018
636. 			[PCNT_POPEST18PLUS] => 77.5
637. 		)
638. 
639. 	[55] => Array
640. 		(
641. 			[SUMLEV] => 
642. 		)
643. 
644. )
645. </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.