An array is a special variable, which can hold more than one value at a time or you can say
An array stores multiple values in one single variable.
If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:
array()
function is used to create an arrayExample:-
$cars = array("Volvo", "BMW", "Toyota");
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
- Indexed arrays - There are two ways to create indexed arrays:
1) $cars = array("Volvo", "BMW", "Toyota");
2)
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
- Associative arrays - Associative arrays are arrays that use named keys that you assign to them.
There are two ways to create an associative array:
1) $age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
2) $age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
A multidimensional array is an array containing one or more arrays.
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);