What does PHP Array mean and Types of Array with Explanation?

PHP Array is an ordered map (contains the value on the basis of the key). An array stores multiple values in one single variable.

An Array can store the multiple values or one or similar type of values in one single variable, for example, suppose that you want to store 100 numbers then instead of defining 100 variables then you can easily define an array of 100 lengths.

What is an array of definition and types?

A variable is a storage area holding only a text or number, an array the problem is that a variable will hold only one value.
therefore, An array is a special variable, which can store multiple types of values in one single variable, the array is actually an ordered map.
it associates values to keys and it is a set of data represented by a single variable name.

PHP Array Example
Every an Array element consists of a value, index or a key that is used for the purpose to access the element.

therefore, internally, all arrays in PHP are associative = hashes…

PHP Array Types

There are 3 types of an array in PHP.

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

PHP Indexed Array

PHP index is represented by a number which starts from 0. We can store number, string, an object in the PHP arrays. All PHP arrays elements are assigned to an index number by default.

There are two ways to define indexed array:

First Way-

2nd Way-

PHP Example-

Output-

Example-

Output-

PHP Associative Array

We can associate a name with each an array elements in PHP using the => symbol.

There are two ways to define an associative array:

1st way:

2nd Way:

PHP Example 1-

Output:

PHP Example 2-

Output:-

PHP Multidimensional Array

A multi-dimensional array of each element in the main array can also be an array, and each element in the sub-array can be array, and so on. the values in the multidimensional array are accessed using multiple indexes.

An array that contains single or multiple arrays within it and it can be accessed via multiple indexes.

Output:-

 

The advantage of PHP Array

Less Code: We don’t need to define multiple variables.
Easy to traverse: With the help of a single loop, we can traverse all the elements of an array.
How to Create PHP Array
Sorting: We can sort the elements of the array.

Resource
PHP.net