An array uses just one variable and lets you save multiple pieces of data using the same variable. That means one can save multiple records in the same way you might save multiple rows in a field, and then fetch each one by using the number in the array that it has been stored against.
When you’re creating variables to save values in your WordPress code, there may come a time when you will find array more efficient to use.
It lets you save multiple pieces of data using just one variable. That means you can now save multiple records in the same way you might save multiple rows in a field, and then fetch each one by using the number in the array that it’s been stored against.
There are three types of array functions that can be created in PHP:
- Indexed – use a numeric key for accessing values
- Associative – It uses text or string keys to access values
- Multidimensional – It contains more than one array
Indexed arrays –
The index can be assigned automatically (index always starts at 0), like this:
|$brands = array(“Nike”, “Puma”, “Skechers”);|
Or it can be assigned manually as:
$brands = “Nike”;
$brands = “Puma”;
$brands = “Skechers”;
Associative arrays –
These are the arrays that use named keys that you assign to them.
The two ways to create an associative array are –
|$value = array(“X”=>”35”, “Y”=>”37”, “Z”=>”43”);|
$value[‘X’] = “35”;
$value[‘Y’] = “37”;
$value[‘Z’] = “43”;
Multidimensional arrays –
These are the arrays containing one or more than one arrays.
These are a bit advanced and you can get to know further about it here in Multidimensional arrays
A simple example of an Array
You are writing a plugin, and you need to create a variable called $wp
If it would be a single static value, you would create it like this –
- $wp = ‘text’;
But if you want to add multiple values in your variable you do it by creating an array –
- $cmses = array( ‘WordPress’, ‘Joomla’, ‘Drupal’ );
To call one of the elements in your array, you use it’s number in the array, starting with zero. Thus to fetch the ‘WordPress’ variable and assign it to another variable called $bestcms, you would use this –
- $bestcms = $cmses;
The Array Toolkit provides support for portfolio items, testimonials, galleries, widgets and more to supported Array themes. Simply install the plugin and the theme will determine which features to activate based on theme support. See your theme’s help file to determine whether or not you need the Array ToolKit. To install Array ToolKit plugin, refer here Array ToolKit.