Chapter 7. Working with Arrays

In this lesson you will learn how to use arrays in PHP to store and retrieve indexed data.

What Is an Array?

An array is a variable type that can store and index a set of values. An array is useful when the data you want to store has something in common or is logically grouped into a set.

Creating and Accessing Arrays

Suppose you wanted to store the average temperature for each month of the year. Using single-value variables—also known as scalar variables—you would need 12 different variables—$temp_jan, $temp_feb, and so on—to store the values. By using an array, you can use a single variable name to group the values together and let an index key indicate which month each value refers to.

The following PHP statement declares ...

Get Sams Teach Yourself PHP in 10 Minutes now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.