A Comprehensive Guide To Arrays In PHP | Everything You Need To Know
Table of Contents
PHP started out as a small open-source project that is embedded in HTML. It is integrated with several databases and supports major protocols. An array holds a significant place in PHP. It actually an ordered map.
It is important for youngsters, who plan to make a career in the field, to be aware of the arrays and its uses. With time you will get a better and deeper understanding about arrays, you can make the best of it. Let’s start to know everything about arrays in PHP by defining what an array is.
What is an Array?
Array in PHP is a type of data structure that allows us to store multiple elements of similar data types under a single variable. The PHP array helps to create a list of elements of similar types, which we can be accessed using their index number.
Suppose we want to store eight names or any value and print them accordingly. This can be easily done by the use of eight different variables. But if instead of eight, the number rises to a hundred or more, then it would be really difficult for the user or developer to create many different variables. So now here, array comes and help us to store every element within a single variable and also allows easy access using an index number. An array is created using an “array ()” function in PHP.
Advantages of using Array:
- In a PHP array, we don’t need to define multiple variables.
- We can traverse all the array elements By using a single loop.
- In PHP we can sort all elements of an array.
- In PHP we can fetch all elements base on their index number or key number at any time.
Array Types in PHP:
PHP has three different types of arrays:
- Indexed or Numeric Array.
- Associative Array.
- Multidimensional Array.
-
Index Array or Numeric Array:
In PHP indexed array or numeric array is a simple array in which data elements are stored in numeric indexes. All the array elements are stored by an index number, which is a numeric value starting from ‘0’ for the first array element.
Creating an Index Array or Numeric Array:
There are two different ways of creating an indexed or numeric array in PHP.
- The syntax for the 1st way to create an indexed array or numeric array in PHP:
$names = array(“shreya”, “subir”, “pritam”,”suman”);
- The syntax for the 2nd way to create an indexed array or numeric array in PHP:
$names[0]=”shreya”;
$names[0]=”subir”;
$names[0]=”pritam”;
$names[0]=”suman”;
Example for 1st way to create an indexed array:
Output: Names are: shreya, subir, pritam and suman.
Example for 2nd way to create an indexed array:
Output: Names are: Shreya, Subir, Pritam and Atanu.
Traversing Array Element using Loop in PHP:
We can traverse an indexed or numeric array using a loop in PHP. Let’s look at an example used for each loop.
Output: Pritam Subir Ritam Shreya.
-
Associative Array in PHP
These kinds of an array are identical to indexed arrays. But in place of linear storage, each value can be designated with a user-specified key of string type.
There are two ways to declare an associative array in PHP:
- Syntaxfor the 1st way to create Associative array in PHP:
$age=array(“shreya”=>”35”,”pritam”=>”20”,”susant”=>”25”,”subir”=>”30”);
- Syntaxfor the 2nd way to create Associative array in PHP:
$age[“shreya”]=”35”;
$age[“pritam”]=”20”;
$age[“susant”]=”25”;
$age[“subir”]=”30”;
Example for 1st way to create Associative array:
Output: shreya age: 35 pritam age: 20 susant age: 25 subir age: 30.
Example for 2nd way to create Associative array:
Output : shreya age: 35 pritam age: 20 susant age: 25 subir age: 30.
Traversing Array Element using Loop in PHP:
We can traverse an Associative array using a loop in PHP. Let’s look at an example used for each loop to traverse an Associative array.
<?php
foreach ($array_name as $key => $value){
// executed
}
?>
Here, for each loop in PHP assigns the key and value of the next element to the variable ($key and $value) that follows the “as” keyword. If the last element is reached PHP ends the loop.
Output:
The capital city of India is Delhi.
The capital city of France is Paris.
The capital city of Germany is Berlin.
The capital city of the United Kingdom is London.
The capital city of the United States is Washington.
-
Multidimensional Array.
In PHP, a multidimensional array is also known as an array of arrays. It accumulates tabular stats in an array. Multidimensional array in PHP can be represented in the form of a matrix, which is represented by row and column. The multi-dimensional array is accessed using multiple indexes.
Syntax for create Multidimensional array in PHP:
$cars = array (
array(“bjaja”,22,18),
array(“tata”,15,13),
array(“safari”,5,2),
array(“Royal”,17,15)
);
Example for creating Multidimensional array in PHP:
Output:
BAJAJ: In stock: 22, sold: 18.
TATA: In stock: 15, sold: 13.
SAFARI: In stock: 5, sold: 2.
ROYAL: In stock: 17, sold: 15
Conclusion:
Arrays are extremely powerful data type, and the challenge that most students and new PHP developers face is to quickly determine the array size in PHP. It is a useful skill that you need to develop over time.
The best way to do so is by taking a proper PHP training under expert guidance. It gives you the exposure to learn the entire programming language and get ready to jump straight to the professional market with amazing skills and in-depth knowledge.
Also Read,