TSV File to PHP Array

This function lets you take a tab separated text file (or comma separated, pipe separated, etc.) and convert it into a PHP array with just one line of code. Huge time saver.

It's similar to the CSV to Array function that we posted, but different in that it doesn't use PHP's built-in fgetcsv function. This gives you some extra flexibility, including the option to open a TSV file instead of a CSV file.

$args is an optional array that you can pass in to set options. If you set $args['tab'] to "," the file can handle a CSV file instead of TSV. Change it to "|" and it will handle pipe separated text. You get the idea…

The other items in the $args array should be fairly self-explanatory or have inline comments to help you out.

Here's the function itself:

<?php

// TSV to Array Function
// Copyright (c) 2015, Ink Plant
// https://inkplant.com/code/
// this version was last updated June 23, 2015

function tsv_to_array($file,$args=array()) {
	//key => default
	$fields = array(
		'header_row'=>true,
		'remove_header_row'=>true,
		'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
		'tab'=>"\t", //tab character
	);
	foreach ($fields as $key => $default) {
		if (array_key_exists($key,$args)) { $$key = $args[$key]; }
		else { $$key = $default; }
	}

	if (!file_exists($file)) {
		if ($debug) { $error = 'File does not exist: '.htmlspecialchars($file).'.'; }
		else { $error = 'File does not exist.'; }
		custom_die($error);
	}

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

	if (($handle = fopen($file,'r')) !== false) {
		$contents = fread($handle, filesize($file));
		fclose($handle);
	} else {
		custom_die('There was an error opening the file.');
	}

	$lines = explode($lb,$contents);
	if ($debug) { echo '<p>Reading '.count($lines).' lines&hellip;</p>'; }

	$row = 0;
	foreach ($lines as $line) {
		$row++;
		if (($header_row) && ($row == 1)) { $data['headers'] = array(); }
		else { $data[$row] = array(); }
		$values = explode($tab,$line);
		foreach ($values as $c => $value) {
			if (($header_row) && ($row == 1)) { //if this is part of the header row
				if (in_array($value,$data['headers'])) { custom_die('There are duplicate values in the header row: '.htmlspecialchars($value).'.'); }
				else {
					if ($trim_headers) { $value = trim($value); }
					$data['headers'][$c] = $value.''; //the .'' makes sure it's a string
				}
			} elseif ($header_row) { //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
				$data[$row][$c] = $value;
			}
		}
	}

	if ($remove_header_row) {
		unset($data['headers']);
	}

	if ($debug) { echo '<pre>'.print_r($data,true).'</pre>'; }
	return $data;
}

?>

Just click the Download link, save that file, include it and then use something like this to generate your array:

//load the function
require_once 'tsv-to-array.inc.php';

//open up your file and convert it to an array
$file = 'my_file.tsv';
$data = tsv_to_array($file,array('header_row'=>true,'remove_header_row'=>true));

//this will print out the array to the screen
echo '<pre>'.print_r($data,true).'</pre>';

That's all there is to it. If you run into problems because your file is too big, I recommend switching to a CSV (if possible) and using our CSV to Array PHP function instead – it can handle bigger files.


Comments

Loading…

This post was first published on May 4th, 2015 and last updated on June 23rd, 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.