Arrays
Explore PHP's versatile arrays, which can serve as both numbered lists and key-value stores. Perfect for handling structured data.


Arrays are one of PHP's most powerful features. If you're coming from JavaScript, you'll find them particularly interesting as they combine the functionality of both JavaScript arrays and objects. While PHP also uses objects for holding data (which we'll explore in our Classes video), arrays are incredibly versatile and widely used. Let's explore how they work.

Basic Array Types

In PHP, we have two main types of arrays. First, there are numeric arrays (similar to JavaScript arrays), which use numbers as their keys starting from 0. Then we have associative arrays, which use named keys (similar to JavaScript objects). The great thing about PHP arrays is that they can also mix both styles.

Let's look at some examples:

Creating Arrays

PHP gives us multiple ways to create arrays. While the square bracket syntax [] is most common in modern PHP, you might also see the array() syntax in older code:

Working with Arrays

Accessing array elements is straightforward. For numeric arrays, we use the index number, and for associative arrays, we use the key name:

Arrays can also be nested, allowing you to create more complex data structures:

Common Operations

PHP provides many ways to work with arrays. Here are some operations you'll use frequently

Real World Example

Let's look at a practical example that you might encounter when working with data in a web application:

This structure shows how arrays can handle complex data with different types and nested information - something you'll often use when working with databases, APIs, or configuration files.