Array

An array is a data structure that linearly stores data, meaning that the data is stored in sequential order. In WordPress, arrays are used to store information such as posts, pages, and settings. Arrays can be single-dimensional, meaning they only have one row of data, or multi-dimensional, meaning they have multiple rows of data.

When you create a new WordPress site, the default settings are stored in an array. This array is then used to display the site’s front page, title, and tagline. The settings array can be modified by adding or removing items or by changing the order of items.

In addition to settings, arrays are also used to store information about posts and pages. Each post and page has its array that contains data such as the title, content, author, date, etc. These arrays are then used to display the post or page on the website.

Types of Arrays

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multi-dimensional arrays – Arrays containing one or more arrays

Indexed Arrays

An indexed array stores data in a linear fashion, meaning that the data is stored in sequential order. Indexed arrays are created using the array() function. The following is an example of an indexed array:

$array = array( "value1", "value2", "value3" );

As you can see, each value in the array has a numeric index. The first value has an index of 0. The second value has an index of 1, and so on.

To access a value in an indexed array, you can use square brackets ([]) and the index of the value you want to retrieve. For example, if we wanted to retrieve the second value from our $array variable, we would use the following code:

$value = $array['1'];

Associative Arrays

An associative array is an array with named keys. Associative arrays are created using the array() function. The following is an example of an associative array:

$array = array( "key1" => "value1", "key2" => "value2", "key3" => "value3" );

As you can see, each value in the array has a named key. The key can be any string, but it must be unique within the array. To access a value in an associative array, you can use square brackets ([]) and the key of the value you want to retrieve. For example, if we wanted to retrieve the value associated with the “key2” key, we would use the following code:

$value = $array['key2'];

Multi-dimensional Arrays

A multi-dimensional array is an array that contains one or more arrays. Multi-dimensional arrays are created using the array() function. The following is an example of a multi-dimensional array:

$array = array(
"array1" => array( "value1", "value2", "value3" ),
"array2" => array( "value4", "value5", "value6" )
);

As you can see, each value in the $array variable is itself an array. To access a value in a multi-dimensional array, you can use multiple square brackets ([][]). For example, if we wanted to retrieve the second value from the “array1” array, we would use the following code:

$value = $array['array1']['1'];
Share via
Copy link