What is PHP Arrays
Arrays are complex variables that allow us to store more than one value or a group of values under a single variable name. Let’s suppose you want to store colors in your PHP script. Storing the colors one by one in a variable could look something like this:
Example:-
<?php | |
$color1 = "Red"; | |
$color2 = "Green"; | |
$color3 = "Blue"; | |
?> |
An array stores multiple values in one single variable:
Example:
<?php | |
$cars = array("Volvo", "BMW", "Toyota"); | |
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; | |
?> |
Create an Array in PHP
In PHP, the array()
function is used to create an array:
array(); |
But what, if you want to store the states or city names of a country in variables and this time this not just three may be hundred. It is quite hard, boring, and bad idea to store each city name in a separate variable. And here array comes into play.
Types of Arrays in PHP
There are three types of arrays that you can create. These are:
- Indexed array — An array with a numeric key.
- Associative array — An array where each key has its own specific value
- Multidimensional arrays – Arrays containing one or more arrays
Indexed Arrays
An indexed or numeric array stores each array element with a numeric index. The following examples shows two ways of creating an indexed array, the easiest way is:
Example:-
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
$cars = array("Devops", "School", "Tutorial"); | |
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; | |
?> | |
</body> | |
</html> |
Associative Arrays
In an associative array, the keys assigned to values can be arbitrary and user defined strings. In the following example the array uses keys instead of index numbers:
Example:-
<!DOCTYPE html> | |
<html> | |
<body> | |
<?php | |
$age = array("Rajesh"=>"35", "Ben"=>"37", "Joe"=>"43"); | |
echo "Rajesh is " . $age['Rajesh'] . " years old."; | |
?> | |
</body> | |
</html> |
Multidimensional Arrays
The multidimensional array is an array in which each element can also be an array and each element in the sub-array can be an array or further contain array within itself and so on. An example of a multidimensional array will look something like this:
Example:-
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Multidimensional Array</title> | |
</head> | |
<body> | |
<?php | |
// Define nested array | |
$contacts = array( | |
array( | |
"name" => "Dharmu kite", | |
"email" => "dharmendra.cotocus1@mail.com", | |
), | |
array( | |
"name" => "Clark Kent", | |
"email" => "clarkkent@mail.com", | |
), | |
array( | |
"name" => "Harry Potter", | |
"email" => "harrypotter@mail.com", | |
) | |
); | |
// Access nested value | |
echo "Dharmu kite Email-id is: " . $contacts[0]["email"]; | |
?> | |
</body> | |
</html> |
Viewing Array Structure and Values
You can see the structure and values of an array by using one of two statements — var_dump()
or print_r()
. The print_r()
the statement, however, gives somewhat less information. Consider the following Example:
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP View Array Structure</title> | |
</head> | |
<body> | |
<?php | |
// Define array | |
$cities = array("London", "Paris", "New York"); | |
// Display the cities array | |
Print_r($cities); | |
?> | |
</body> | |
</html> |
The print_r()
statement gives the following output:Array ( [0] => London [1] => Paris [2] => New York )
This output shows the key and the value for each element in the array. To get more information, use the following statement:
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<title>PHP Get Complete Array Information</title> | |
</head> | |
<body> | |
<?php | |
// Define array | |
$cities = array("London", "Paris", "New York"); | |
// Display the cities array | |
var_dump($cities); | |
?> | |
</body> | |
</html> |




I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND