An array is a variable that can hold more than one value under a single name, which can be accessed by a text key or an index number. It is used in PHP language and WordPress is based in PHP, So most of the core sections of WordPress, Themes, and plugin contain arrays.
Types of the array you can make in WordPress?
- Indexed – use numeric keys to access values
- Associative – use text or string keys to access values
- Multidimensional – contain more than one array
You can use these array types to create functions on the WordPress core.
How to use Array in WordPress?
You can use an array in WordPress to shorten the code by containing more variables into a single line.
For example.
$Category1 = "WordPress";
$Category2 = "SEO";
$Category3 = "SEM";
Which can be reduced to,
Now, this function can be used to perform operations on data. Functions like count() can give the required category selected and get categories can choose a category for function.
get_categories( string|array $args = '' )
array starts from 0.
Working Example of Array in WordPress
<?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>
$args variable is an array, storing number of arguments. Which is then passed to wp_list_categories.