What is an Array in PHP?
An array is PHP’s ordered-map data type: a collection of values, each associated with an integer or string key. WordPress core, themes, and plugins use arrays constantly to pass settings, query parameters, and lists of data. You create one with the array() language construct or the shorter [] syntax, and usually assign it to a single variable.
More About Arrays
An array is the ordered-map data type in PHP (Hypertext Preprocessor): it stores multiple values as key => value pairs, so related data travels together instead of sitting in separate variables. You'll usually see an array assigned to a single variable, such as $args, but an array doesn't need a name to exist: PHP code can pass one straight into a function, return one, or nest one inside another array. The array() language construct creates one, and so does the shorter square-bracket syntax: array( 'foo' => 'bar' ) and [ 'foo' => 'bar' ] build the same array. PHP is the main programming language WordPress is built on; themes and plugins combine it with HTML, CSS, and JavaScript.
How WordPress code uses arrays
Many WordPress APIs accept settings as associative arrays. WP_Query, the class WordPress uses to fetch posts, is one common example:
$args = array( 'post_type' => 'post', 'posts_per_page' => 5 );
$query = new WP_Query( $args );
Each 'key' => value pair is one named setting: this query asks for standard posts, 5 per page, and $args['posts_per_page'] fetches a single value back by its key. That reading skill is the practical payoff. Change the 5 to a 10 and the copied snippet returns 10 posts, and the same pattern runs through most code you'll paste into functions.php. For the language basics behind it, our guide How to Learn PHP (Fast & Free) is a good next step.
Types of arrays
PHP has one array type (under the hood, an ordered map), and developers use it in three common shapes:
- Indexed: you give no keys, so PHP numbers the items automatically starting at 0, as in
[ 'Monday', 'Tuesday' ]. - Associative: you name each key yourself, as in
'post_type' => 'post'. This is the shape WordPress uses for settings. - Multidimensional: an array whose values are themselves arrays, used for nested data such as a list of posts where each post carries its own fields.
These labels describe how the keys are used, not separate types. The PHP manual is explicit that PHP does not distinguish between indexed and associative arrays, and a single array can hold int and string keys at the same time. So when you meet an array in a theme or plugin file, read its keys first: they tell you whether you're looking at a simple list or a set of named settings.
Frequently Asked Questions
- A variable is a named container that holds one value. An array is a data type that holds many values, each stored under its own key. Assign an array to a variable, like $args, and every setting a WordPress query needs travels under one name.
- Use array() or square brackets: array( 'a', 'b' ) and [ 'a', 'b' ] build the same two-item array. Name keys with =>, as in [ 'color' => 'blue' ]; skip them and PHP numbers items from 0. A trailing comma after the last item is valid.
- Use foreach: foreach ( $items as $key => $value ) { ... } runs your code once per item. Loop when every item needs processing; when you only need one value, fetch it directly by its key instead, like $args['post_type'].
- Reading a missing key triggers a PHP warning (a notice before PHP 8.0), not a crash: the code keeps running, but the value is missing. Check first with isset() or array_key_exists(), and match documented keys exactly.
Powerful WordPress Hosting
Reliable, lightning-fast hosting solutions specifically optimized for WordPress. Find the perfect plan for you by clicking below.
WordPress Hosting Plans