Arrays are compound variables that allow storing values or a group of related values to store in a single variable name.
Why we need Array?
Let’s suppose you have a list of 4 student’s and you want to store those names in the PHP script. If you want to store those name in single variable could look something like this.
<?php $student1 = "Sagal"; $student2 = "Sajal"; $student3 = "Lalan"; $student4 = "Anamitra"; ?>
What if you want to store names of 400 students instead of just 4. It is quite hard to define and work with 400 variables and finding a specific name will be more complex. Here the array comes into play.
Types of Array in PHP
There are three types of array that you can create in PHP. They are:
- Indexed arrays – An arrays with a numeric index
- Associative arrays – An arrays with named keys
- Multidimensional arrays – An arrays containing one or more arrays
PHP Indexed Array
PHP indexed array stores each array element with a numeric index starting from 0. We can store different kinds of data like number, string, or even object in the array.
There are two ways by which we can define an indexed array.
Way 1: (The easiest Way)
<?php //defined an indexed array $student=array("Sagar","Sajal","Lalan","Anamitra"); ?>
Way 2:
<?php $student[0] = "Sagar"; $student[1] = "Sajal"; $student[2] = "Lalan"; $student[3] = "Anamitra"; ?>
Example
<?php //defined an indexed array $student=array("Sagar","Sajal","Lalan","Anamitra"); echo "Students names are: $student[0], $student[1], $student[2], $student[3]"; ?>
Output
If you use the 2nd method of defining the indexed array:
<?php $student[0] = "Sagar"; $student[1] = "Sajal"; $student[2] = "Lalan"; $student[3] = "Anamitra"; echo "Students names are: $student[0], $student[1], $student[2], $student[3]"; ?>
Output
PHP Associative Array
In an associative array, associative name is used with each array elements in PHP using => symbol.
There are also two ways by which we can define an associative array.
1st way:
<?php //defined an indexed array $salary=array("Sagar"=>"140000","Sajal"=>"80000","Lalan"=>"45000","Anamitra"=>"85000"); ?>
2nd Way:
<?php $salary["Sagar"] = "140000"; $salary["Sajal"] = "80000"; $salary["Lalan"] = "45000"; $salary["Anamitra"] = "85000"; ?>
Example
<?php //defined an indexed array $salary=array("Sagar"=>"140000","Sajal"=>"80000","Lalan"=>"45000","Anamitra"=>"85000"); echo "Sagar salary: ".$salary["Sagar"]."<br/>"; echo "Sajal salary: ".$salary["Sajal"]."<br/>"; echo "Lalan salary: ".$salary["Lalan"]."<br/>"; echo "Anamitra salary: ".$salary["Anamitra"]."<br/>"; ?>
Output
Sajal salary: 80000
Lalan salary: 45000
Anamitra salary: 85000
Example
<?php $salary["Sagar"] = "140000"; $salary["Sajal"] = "80000"; $salary["Lalan"] = "45000"; $salary["Anamitra"] = "85000"; echo "Sagar salary: ".$salary["Sagar"]."<br/>"; echo "Sajal salary: ".$salary["Sajal"]."<br/>"; echo "Lalan salary: ".$salary["Lalan"]."<br/>"; echo "Anamitra salary: ".$salary["Anamitra"]."<br/>"; ?>
Output
Sajal salary: 80000
Lalan salary: 45000
Anamitra salary: 85000
PHP Multidimensional Array
The multidimensional-array is an array in which each element inside an array can be an array and each element in subarray can be an array and so on. An example of a multidimensional-array can be some look as below.
Example
<?php // Define a multidimensional array $emp = array( array( "name" => "Sagar Sharma", "salary" => "140000", ), array( "name" => "Sajal Mohanta", "salary" => "80000", ), array( "name" => "Lalan Murari", "salary" => "45000", ) ); // Access nested value echo "Sajal's Salary is: " . $emp[1]["salary"]; ?>
Output