---
title: "Read a CSV to Array in PHP"
url: https://daily.dev/posts/read-a-csv-to-array-in-php-vyo985jbc
source_url: https://code.tutsplus.com/articles/read-a-csv-to-array-in-php--cms-39471
type: article
source: "Tuts+"
published: 2021-12-31T04:27:49.061Z
updated: 2021-12-31T04:27:49.062Z
tags: ["php"]
reading_time: 5
upvotes: 11
comments: 2
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Read a CSV to Array in PHP

**[Tuts\+](https://daily.dev/sources/tuts_plus)** · 5 min read · 11 upvotes · 2 comments

## Summary

In this article, we'll learn how to read and print a simple CSV file. We'll use fopen() and fgetcsv() to read the contents of a CSV file, then we'll convert it into an array using the array_map() and str_get CSV() functions. The words found in the file are then looped through and printed into an HTML table.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://code.tutsplus.com/articles/read-a-csv-to-array-in-php--cms-39471>

## Community discussion

Top comments from developers on daily.dev.

**@cyberjack** · 2 upvotes

> Memory heavy ways to read CSV files. When the files are too big, these methods will most likely cause some problems.
>
>
> On top of that, the chances that you need a line read from CSV more than once, are slim to zero. Storing everything into an array is plain overkill and is therefore almost never needed.
>
>
> To solve this in a memory friendly way, you can use the CSV reader in [SplFileObject](https://www.php.net/manual/en/splfileobject.fgetcsv.php), which uses an iterator. You can also modify the `csvToArray` function from the 2nd example and start using a...

**@t0ny** · 1 upvotes

> Looks more like a how-to for creating a xss vulnerability. Probably worth calling out how bad it would be to use that code without sanitizing the html table data.

## Similar posts on daily.dev

- [Don’t just attend KubeCon \+ CloudNativeCon, Merge Forward your experience\!](https://daily.dev/posts/don-t-just-attend-kubecon-cloudnativecon-merge-forward-your-experience--l0rpp73x8) · CNCF · 0 upvotes · 0 comments
- [Announcing H2 2026 KCDs](https://daily.dev/posts/announcing-h2-2026-kcds-m96goajm1) · CNCF · 1 upvotes · 0 comments
- [Two months of Open Community Groups](https://daily.dev/posts/two-months-of-open-community-groups-asf52zhbs) · CNCF · 0 upvotes · 0 comments
- [CNCF Unveils Schedule for KubeCon \+ CloudNativeCon Europe 2026](https://daily.dev/posts/cncf-unveils-schedule-for-kubecon-cloudnativecon-europe-2026-ikhcoa5cb) · CNCF · 2 upvotes · 0 comments
- [CNCF Debuts KubeCon \+ CloudNativeCon Japan 2026 Schedule](https://daily.dev/posts/cncf-debuts-kubecon-cloudnativecon-japan-2026-schedule-xp5pyudub) · CNCF · 1 upvotes · 0 comments

---

Tags: [#php](https://daily.dev/tags/php)

[View this post on daily.dev](https://daily.dev/posts/read-a-csv-to-array-in-php-vyo985jbc)
