Parsing Character Delimited Files into Arrays

In this tutorial you will learn how to parse a multiple-line character delimited text file into a two dimensional array using PHP.

There are various reasons for which you might want to parse a character delimited file into an array, and this code will help you do just that. The character that’s delimiting the values is of no relevance to the code, it could be a tab, space, pipe or any other character from the ASCII set. The sample file we are working with has the following content:

A B C
D E F
G H I

As you can see there are two types of delimitations here. There are the lines, that are being delimited by a new line character (“\r\n” in Windows, “\n” in Unix), and then in each line, the values are being delimited by a tab (“\t”). In our code we’re first going to delimit the file into lines, and while we loop through those lines we split each value apart using the explode() function. This will create a second dimesion to the array where the values are being stored.

Let’s start with the first part, preparing the variables:

// This is what the sample file contains:
// A B C
// D E F
// G H I

// File location (URL or server path)
$FilePath = "SampleFile.txt";
// Stores the content of the file
$Lines = file($FilePath);
// Counts the number of lines
$LineCount = count($Lines);
// This will be a two dimensional array that holds the content nicely organized
$Data = array();
// We will use this as an index
$i = 0;

The file path that’s being specified can be an absolute path, relative path or URL. The line count is important so that we loop through all the lines in the file. Following that an array is created, that will be our most important object in this piece of code.

Now comes the loop that does it all:

// Loop through each line
foreach($Lines as $Value)
{
   // In the array store this line with values delimited by \t (tab) as separate array values
   $Data[$i] = explode("\t", $Value);
   // Increase the line index
   $i++;
}

What happens here is that when we use the foreach loop combined with the $Lines variable that holds the content of the file, we are actually looping through each line. That’s because PHP is smart enough to realize what our objective is. Since we loop through each line, we don’t just satifsfy with storing one line per index into a one dimensional array, but further we make use of the explode() function, which will take two parameters, the character by which we want to split, and the string that we want to split. In this case the string is the current line, and the delimiting character is a tab.

The final lines of code will just extract three sample values from our newly created array for demonstration purposes:

// Will return A
echo "First row, first column: ".$Data[0][0]."<br />";
// Will return E
echo "Second row, second column: ".$Data[1][1]."<br />";
// Will return G
echo "Third row, first column: ".$Data[2][0]."<br />";

The beauty of this code is its simplicity and the minimum need of modification in order to adapt it to your own needs. It has no set limits for the number of rows or columns that the array can hold, and no need to specify the size of the array.

Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top