Sai A Sai A
Updated date Oct 11, 2023
In this blog, we will discover how to convert a CSV file to an array in PHP. Explore three distinct methods—fgetcsv(), str_getcsv(), and file() with str_getcsv()—accompanied by practical examples and outputs.

Introduction:

Efficient data handling is a cornerstone of web development, and CSV (Comma Separated Values) files serve as a common ground for data exchange. PHP, a robust server-side scripting language, provides various approaches to convert CSV files into arrays, empowering developers to manipulate data seamlessly. In this blog, we will explore multiple methods for CSV to array conversion in PHP.

Method 1: Using fgetcsv() Function

The fgetcsv() function is a PHP native that reads a line from a file, interprets it as CSV, and returns the values as an array. Let's dive into a practical example:

<?php
$file = fopen('data.csv', 'r');
while (($row = fgetcsv($file)) !== false) {
    $csvData[] = $row;
}
fclose($file);

// Output
print_r($csvData);
?>

Output:

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

    [1] => Array
        (
            [0] => John
            [1] => 25
            [2] => Developer
        )

    [2] => Array
        (
            [0] => Jane
            [1] => 30
            [2] => Designer
        )
)

This method reads the CSV file line by line, utilizing fgetcsv() to parse each line into an array, and finally, consolidates these arrays into $csvData.

Method 2: Using str_getcsv() Function

An alternative approach involves the str_getcsv() function, which parses a CSV string directly. This is particularly useful when dealing with CSV data stored as a string:

<?php
$csvString = "Name,Age,Occupation\nJohn,25,Developer\nJane,30,Designer";
$csvData = array_map('str_getcsv', explode("\n", $csvString));

// Output
print_r($csvData);
?>

Output:

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

    [1] => Array
        (
            [0] => John
            [1] => 25
            [2] => Developer
        )

    [2] => Array
        (
            [0] => Jane
            [1] => 30
            [2] => Designer
        )
)

Here, the CSV string is split into an array of lines using explode(), and str_getcsv() is applied to each line via array_map().

Method 3: Using file() and str_getcsv()

Combining the file() function with str_getcsv() provides a concise method for reading entire CSV files at once:

<?php
$lines = file('data.csv', FILE_IGNORE_NEW_LINES);
$csvData = array_map('str_getcsv', $lines);

// Output
print_r($csvData);
?>

Output:

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

    [1] => Array
        (
            [0] => John
            [1] => 25
            [2] => Developer
        )

    [2] => Array
        (
            [0] => Jane
            [1] => 30
            [2] => Designer
        )
)

In this method, file() reads the CSV file into an array of lines, and str_getcsv() is applied to each line using array_map().

Conclusion:

In this blog, we have explored three distinct methods for converting CSV files to arrays in PHP. Whether you opt for the line-by-line approach of fgetcsv(), the string parsing prowess of str_getcsv(), or the combined power of file() and str_getcsv(), each method has its merits.

Comments (0)

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