An array is the most important part of any programing language. It is very useful if you work on a large amount of data or fetching and manipulating the database.
In this article, the following topic will be covered:
- What are Arrays
- Usage of Arrays in PHP
- Types of Array
- Indexing Arrays
- Associative Arrays
- Multidimensional Arrays
- Operating on Arrays
- Sorting Arrays
What are PHP Arrays?
A PHP array is a variable that store single or multiple data type values in a single variable and that can access by index or associate’s name. An array creates using array() function or sqare[] brackets.
Example:
1 2 3 4 | <?php $names = array('Name1', 'Name2', 'Name3'); $names = ['Name1', 'Name2', 'Name3']; //Another way to write array ?> |
Usage of PHP Arrays:
Easy to manipulate: Adding or removing elements in an array, and reading or changing an element’s value, is easy.
A large amount of data: Through the loop, you can easily reading or changing each element’s value.
Manipulation of Array: PHP gives many in-built functions to merge array, sort array value, and key as well and remove elements, etc.
Types of Array
There are three types of Array:
- Index or Numeric Array
- Associative Array
- Multidimensional Array
Indexing Array
An indexed or numeric array stores a numeric index for every array element.
Syntax:
1 2 3 | <?php $name[n] = value; ?> |
or
1 2 3 | <?php $name = array(value, ....); ?> |
Let’s take an example:
1 2 3 4 5 | <?php $name[0] = 'John'; $name[1] = 'Peter'; $name[2] = 'Troposal'; ?> |
Associative Arrays
Associative array associates with key and value.
Let’s take an example:
1 2 3 | <?php $salary = array("John" => 1000, "Peter"=> 8000, "Trop" => 5000); //associative array ?> |
In the above example, names are associated with salary.
Multidimensional Arrays
A multidimensional array is a set of arrays. PHP supports two, three, four, etc level of the multidimensional array. Array with more than three levels is difficult to manage.
Syntax:
1 2 3 4 5 6 7 8 | <?php //Two dimensional array $arrayVar = array( array( value1, value2, value3 ), array( value4, value5, value6 ), array( value7, value8, value9 ) ); ?> |
In the above syntax is a two-dimensional(2D) array.
Let’s take a table of employee and implement it in an array:
Name | Age | Salary |
John | 28 | 50000 |
Peter | 25 | 40000 |
Harry | 26 | 45000 |
1 2 3 4 5 6 7 8 9 10 11 | <?php //Two dimensional array $emp = array( array( 'John', '28', '50000' ), array( 'Peter', '25', '40000' ), array( 'Harry', '26', '45000' ) ); echo $emp[0][0];//Output: John echo $emp[0][1];//Output: 28 echo $emp[1][0];//Output: Peter ?> |
Operating on Arrays
PHP has many built-in functions to operate the array. We will apply some of them for operation and also we will use the PHP loop.
1. Print_r: It is used to print the array.
Example:
1 2 3 4 | <?php $name = array("John","Peter", "Trop"); print_r($name); ?> |
Output:
1 2 3 4 5 6 | Array ( [0] => John [1] => Peter [2] => Trop ) |
2. Var_dump: It shows data type and value as well.
Example:
1 2 3 4 | <?php $var = array("John", 28, 500); var_dump($var); ?> |
Output:
1 2 3 4 5 6 7 8 | array(3) { [0]=> string(4) "John" [1]=> int(28) [2]=> int(500) } |
3. Count: It is used to count the array.
Example:
1 2 3 4 | <?php $var = array("John", 28, 500); echo count($var); ?> |
Output:
1 | 3 |
4. Foreach Loop: Foreach Loop is used to print the array.
Example:
1 2 3 4 5 6 | <?php $empName = array('John', 'Peter', 'Harry'); foreach($empName as $name) { echo $name."\n";//If you are checking code on browser then write "<br>" in place of "\n"; } ?> |
Output:
1 2 3 | John Peter Harry |
You can print Employee name through For loop.
1 2 3 4 5 6 7 | <?php $empName = array('John', 'Peter', 'Harry'); $count = count($empName);//Total count of array: 3 for($i = 0; $i< $count; $i++) { echo $empName[$i]."\n"; } ?> |
Sorting Arrays
Sorting is used to order the data in ascending or descending. PHP has the in-built function for sorting the array.
1.
sort()
: It is used to sort an array in ascending array.
Example:
1 2 3 4 5 | <?php $num = array(30,34,28,20,50); sort($num); print_r($num); ?> |
Output:
1 2 3 4 5 6 7 8 | Array ( [0] => 20 [1] => 28 [2] => 30 [3] => 34 [4] => 50 ) |
2.
rsort()
: It is used to sort an array in descending order
1 2 3 4 5 | <?php $num = array(30,34,28,20,50); rsort($num); print_r($num); ?> |
Output:
1 2 3 4 5 6 7 8 | Array ( [0] => 50 [1] => 34 [2] => 30 [3] => 28 [4] => 20 ) |
3.
asort():
It is used to sort the associative array in ascending order.
1 2 3 4 5 | <?php $salary = array("John" => 1000, "Peter"=> 8000, "Trop" => 5000); asort($salary); print_r($salary); ?> |
Output:
1 2 3 4 5 6 | Array ( [John] => 1000 [Trop] => 5000 [Peter] => 8000 ) |
4.
ksort():
It is used to sort associative in ascending order according to the key.
1 2 3 4 5 | <?php $salary = array("Peter" => 1000, "John"=> 8000, "Trop" => 5000); ksort($salary); print_r($salary); ?> |
Output:
1 2 3 4 5 6 | Array ( [John] => 8000 [Peter] => 1000 [Trop] => 5000 ) |
Check the article to sort array without in-built function.