Sai A Sai A
Updated date Dec 20, 2023
In this blog, we will discover how to work with CSV files in PHP. This blog shows you easy ways to convert CSV files with special separators into lists in PHP. Follow along with practical examples and code bits using methods like str_getcsv, fgetcsv, and SplFileObject.

Introduction:

Handling CSV files in PHP is a common task for developers, but what if your CSV file uses a custom delimiter instead of the standard comma? In this blog, we will explore how to convert a CSV file with a custom delimiter to an array in PHP. We'll provide not just one but multiple methods, ensuring you have a toolkit to handle different scenarios.

Method 1: Exploring the Basics with str_getcsv

The str_getcsv function is a PHP built-in function that parses a CSV string into an array. However, it defaults to using a comma as the delimiter. To handle custom delimiters, we can use the second parameter, which allows us to specify a custom delimiter.

<?php
$fileContents = file_get_contents('your_file.csv');
$lines = explode(PHP_EOL, $fileContents);
$data = array();

foreach ($lines as $line) {
    $data[] = str_getcsv($line, ';'); // Specify your custom delimiter here (e.g., semicolon)
}

print_r($data);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => Name
            [1] => Age
            [2] => Country
        )

    [1] => Array
        (
            [0] => John Doe
            [1] => 28
            [2] => USA
        )

    [2] => Array
        (
            [0] => Jane Smith
            [1] => 35
            [2] => Canada
        )
    // Additional rows...
)

Method 2: Leveraging fgetcsv for Streamlined Parsing

Another method involves using fgetcsv, which reads a line from an open file and parses it as CSV. It automatically handles custom delimiters as long as you specify it using the delimiter parameter.

<?php
$handle = fopen('your_file.csv', 'r');
$data = array();

while (($line = fgetcsv($handle, 0, ';')) !== false) {
    $data[] = $line;
}

fclose($handle);
print_r($data);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => Name
            [1] => Age
            [2] => Country
        )

    [1] => Array
        (
            [0] => John Doe
            [1] => 28
            [2] => USA
        )

    [2] => Array
        (
            [0] => Jane Smith
            [1] => 35
            [2] => Canada
        )
    // Additional rows...
)

Method 3: Advanced Parsing with SplFileObject

For a more object-oriented approach, we can use SplFileObject. This class provides an easy interface for reading files line by line. Custom delimiters can be specified using the setCsvControl method.

<?php
$file = new SplFileObject('your_file.csv');
$file->setFlags(SplFileObject::READ_CSV);
$file->setCsvControl(';');

$data = array();
foreach ($file as $row) {
    $data[] = $row;
}

print_r($data);
?>

Output:

Array
(
    [0] => Array
        (
            [0] => Name
            [1] => Age
            [2] => Country
        )

    [1] => Array
        (
            [0] => John Doe
            [1] => 28
            [2] => USA
        )

    [2] => Array
        (
            [0] => Jane Smith
            [1] => 35
            [2] => Canada
        )
    // Additional rows...
)

Conclusion:

In this blog, we have discussed three different methods to convert a CSV file with a custom delimiter to an array in PHP. Whether you prefer the simplicity of str_getcsv, the streamlining of fgetcsv, or the object-oriented elegance of SplFileObject.

Comments (0)

There are no comments. Be the first to comment!!!