Arrays (intro)
Think of an array as a container that holds multiple values under one name. Instead of creating separate variables like $car1, $car2, and $car3, you can store all three in a single array called $cars. It keeps your code cleaner and makes working with collections of data much easier.
The Three Types of Arrays
PHP gives you three ways to organize data in arrays, and each serves a different purpose:
Indexed arrays use numbers to identify each item. When you create an array like $cars = array("Volvo", "BMW", "Toyota"), PHP automatically assigns numbers starting from 0. So $cars[0] is “Volvo”, $cars[1] is “BMW”, and so on.
Associative arrays use descriptive names (called keys) instead of numbers. These are incredibly useful when you need to label your data. For example, $member = ['name' => 'Alice', 'status' => 'active'] lets you access values by their meaning: $member['name'] gives you “Alice”. This is much clearer than trying to remember that $member[0] is the name and $member[1] is the status.
Multidimensional arrays are arrays that contain other arrays. Imagine a list of members where each member is itself an array of properties. You'll use these constantly in WordPress development.
Important Things to Know
Arrays are flexible. You can store strings, numbers, other arrays, even functions inside them. You can even mix different data types in the same array, though it's usually clearer to keep them consistent.
PHP includes powerful built-in functions for working with arrays. The simplest one is count(), which tells you how many items are in an array. There are dozens more that let you sort, filter, search, and manipulate array data.
WordPress connection: Almost everything in WordPress is an array. Post data? Array. User info? Array. Plugin settings? Array. Query results? Array of arrays.