PHP array is an ordered map (contains value on the basis of key). It is used to hold multiple values of similar type in a single variable.
An array stores multiple values in one single variable.
Create an Array in PHP :
In PHP, the array() function is used to create an array.
In PHP, there are three types of arrays:
- Indexed arrays - Arrays with a numeric index
- Associative arrays - Arrays with named keys
- Multidimensional arrays - Arrays containing one or more arrays
<?php $cars = array("Volvo", "BMW", "Toyota"); echo "Best Car companies are " . $cars . ", " . $cars . " and " . $cars . "."; ?>Output:
Best Car companies are Volvo, BMW and Toyota.
Advantage of PHP Array:
1. Sorting: We can sort the elements of array easily.
2. Less Code: We don't need to define multiple variables.
3. Easy to traverse: By the help of single loop, we can traverse all the elements of an array.
4. Take Less Memory : Array allocate less memory.