In computer programming languages, an array is a special variable that can hold more than one value under a single name. It is possible to then access the values by referring to an index number or a text key.
WordPress is written in the PHP programming language and hence as a WordPress user you may come across them while working on WordPress themes or plugins or by simply looking at the core WordPress code. In PHP, the array() function is used to create them. There are three types that can be created in PHP:
- Indexed – use a numeric keys to access values
- Associative – use text or string keys to access values
- Multidimensional – contain more than one array
Many arrays are used to loop through a set of data and perform some sort of operation on each value. For example if you have three pieces of fruit you could store each as a separate variable like this:
1| $fruits1 = "apple";
2| $fruits2 = "orange";
3| $fruits3 = "banana";
This can quickly get very messy. A better solution would be to put them all in an array like this:
1| $fruits = array("apple", "orange", "banana");
Now you can do things like use built in array functions to perform operations on the data. For example, count() would tell you how many elements are in your array. $fruits would equal ‘banana’ (arrays start at zero).
Examples in WordPress
The $args variable is an array, storing a number of arguments. These are then passed in to the wp_list_categories function later on.
4| 'orderby' => 'name',
5| 'show_count' => 0,
6| 'pad_counts' => 0,
7| 'hierarchical' => 1,
8| 'title_li' => 'Categories'
13| ?php wp_list_categories( $args ); ?>
« Back to Glossary Index