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:
1// Numeric array 2$colors = ['red', 'blue', 'green']; 3 4// Associative array 5$user = [ 6 'name' => 'John', 7 'age' => 25 8]; 9 10// Mixed array11$mixed = [12 42, // index 013 'key' => 'value', // key 'key'14 'another value' // index 115];
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:
1// Modern syntax2$array1 = ['a', 'b', 'c'];3 4// Creating with specific keys5$user = [6 'name' => 'John',7 'age' => 258];
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:
1$colors = ['red', 'blue', 'green'];2echo $colors[0]; // outputs: red3 4$user = ['name' => 'John', 'age' => 25];5echo $user['name']; // outputs: John
Arrays can also be nested, allowing you to create more complex data structures:
1$blogPost = [ 2 'title' => 'Getting Started with PHP', 3 'author' => [ 4 'name' => 'John', 5 'role' => 'editor' 6 ], 7 'comments' => [ 8 ['user' => 'Jane', 'text' => 'Great article!'], 9 ['user' => 'Bob', 'text' => 'Thanks for sharing']10 ]11];12 13echo $blogPost['title']; // outputs: Getting Started with PHP14echo $blogPost['author']['name']; // outputs: John15echo $blogPost['comments'][0]['text']; // outputs: Great article!
Common Operations
PHP provides many ways to work with arrays. Here are some operations you'll use frequently
1// Adding elements 2$colors[] = 'yellow'; // Add to numeric array 4 5// Checking elements 6isset($user['email']); // Check if key exists 7in_array('red', $colors); // Check if value exists 8 9// Array size10count($colors); // Get number of elements11 12// Remove elements13unset($user['email']); // Remove specific element
Real World Example
Let's look at a practical example that you might encounter when working with data in a web application:
1$post = [ 2 'title' => 'Getting Started with PHP', 3 'author' => [ 4 'name' => 'John Doe', 5 'role' => 'editor' 6 ], 7 'tags' => ['php', 'beginner', 'tutorial'], 8 'published' => true, 9 'comments' => [10 [11 'user' => 'Jane',12 'message' => 'Great article!'13 ],14 [15 'user' => 'Mike',16 'message' => 'Thanks for sharing'17 ]18 ]19];
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.
Laravel is the most productive way to
build, deploy, and monitor software.