Arrays in PHP play a very important role in data handling and storing data of the same kind.
What is an Array in PHP ?
An array is basically a variable which stores multiple values of the same kind (datatype).
Advantages of Arrays in PHP
- Can represent and store multiple items of the same type in a single variable.
- Can be used to implement some specific concept of data structures like linked lists, stacks and queues etc.
- Also can be used to represent matrices.
Disadvantages of Arrays in PHP
- Must know in advance the quantity of the elements.
- It is a static structure i.e. it is of fixed size.
- Operations like and insertion and deletion are time-consuming and increase the overall complexity.
Creating Arrays in PHP
Arrays in PHP are created with the help of a predefined function called “array()”.
Arrays in PHP can be implemented in three forms:
- ASSOCIATIVE ARRAYS
- INDEXED ARRAYS
- MULTIDIMENSIONAL ARRAYS
Associative Arrays in PHP
In this type of form of an array, the array uses a “named key” to map the values associated with it.
First of all, one has to assign a key to each and every element/value and then store it in an array. Secondly the value must be pointed by the key using this symbol “=>”.
See the following code and the corresponding output for clarity:-
<?php $examplearray=array(“Peter”=>”35”, “Saurav”=>”37”,”Sushil”=>”43”); echo “ Peter is”.$examplearray[‘peter’].”years old”; ?>
In the above code, one can see the use of a key to refer the value. The keys specified above are “Peter, Saurav and Sushil” for the values “35,37 and 43” respectively.
Now the key Peter is used to map its age and show the output.
Indexed Arrays in PHP
In these type of arrays, the index is automatically assigned and by default starts from 0 (0,1,2..). Firstly here there is no involvement of keys. Secondly, here the internal indexing is used to refer to the vlaue in the array.
See the following code snippets and output for clarity:-
<?php $examplearray=array(“35”, “37”,”43”); echo “ Value at the index 2 is”.$examplearray[2]; ?>
In the above code, you can see the default (inbuilt index) indexing is used to refer the values. Here no key-value mechanism is used. In programming terms, the index starts from 0.
The above code uses an array to store the values “35 37 and 43”. These values are stored at index 0, 1 and 2 respectively. Now we are showing the value at index 2 of the array.
Output:-
Multi-Dimensional Arrays in PHP
In these type of arrays, it stores more than one array within itself i.e. each element can also be an array and each element in sub-array can be an array or further contain within itself.
See the pictures of code snippets and output for clarity:-
<?php $examplearray=array( array(“Name”=>”Aditya”, “age”=>”19”, array(“name”=> “Saurav”, “age”=>”37”, array(“name”=>”Sushil”,“age”=>”43”) ); echo “Aditya’s age is”.$examplearray[0][“age”]; ?>
First of all in the above code, you can see that we can define multiple subarrays inside a single main array and therefore it is called a multidimensional array. Secondly, the code stores the name and age in a key-value format for three people in a main single array. The name and age components are used as a separate sub-array for each of the people.
Output:-
NOTE: In all of the above-mentioned examples, you can traverse through the arrays using loop concepts because an array has multiple values. The most common loop concept used in PHP is the “foreach loop”
Syntax:-
foreach(arrayname as variable(key)=> variable_value) {echo “Key=”.$x.”, value=”.$variable_value;}
If you have any doubts, please mention in the comment section.