What is: Array


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:

  $fruits1 = "apple";  $fruits2 = "orange";  $fruits3 = "banana";  

This can quickly get very messy. A better solution would be to put them all in an array like this:

  $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[2] would equal ‘banana’ (arrays start at zero).

Example 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.

  <?php  $args = array(    'taxonomy'     => 'category',    'orderby'      => 'name',    'show_count'   => 0,    'pad_counts'   => 0,    'hierarchical' => 1,    'title_li'     => 'Categories'  );  ?>    <ul>  <?php wp_list_categories( $args ); ?>  </ul>