What does PHP Array mean and Types of Array with Explanation?

PHP Array is an ordered map (contains the value on the basis of the key). An array stores multiple values in one single variable.

An Array can store the multiple values or one or similar type of values in one single variable, for example, suppose that you want to store 100 numbers then instead of defining 100 variables then you can easily define an array of 100 lengths.

<?php
$phones = array("Samsung", "Nokia", "Sony");
echo "I Most like " . $phones[0] . ", " . $phones[1] . " and " . $phones[2] . ".";
?>

What is an array of definition and types?

A variable is a storage area holding only a text or number, an array the problem is that a variable will hold only one value.
therefore, An array is a special variable, which can store multiple types of values in one single variable, the array is actually an ordered map.
it associates values to keys and it is a set of data represented by a single variable name.

PHP Array Example
Every an Array element consists of a value, index or a key that is used for the purpose to access the element.

therefore, internally, all arrays in PHP are associative = hashes…

PHP Array Types

There are 3 types of an array in PHP.

  1. Indexed Array
  2. Associative Array
  3. Multidimensional Array

PHP Indexed Array

PHP index is represented by a number which starts from 0. We can store number, string, an object in the PHP arrays. All PHP arrays elements are assigned to an index number by default.

There are two ways to define indexed array:

First Way-

$season=array("summer","winter","spring","autumn");

2nd Way-

$season[0]="summer";  
$season[1]="winter";  
$season[2]="spring";  
$season[3]="autumn";

PHP Example-

<?php  
$season=array("March","June","September","December");  
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";  
?>

Output-

Season are: March, June, September and December

Example-

<?php  
$season[0]="summer vacation";  
$season[1]="winter vacation";  
$season[2]="spring vaction";  
$season[3]="autumn vaction";  
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";  
?>

Output-

Season are: summer vacation, winter vacation, spring vacation and autumn vacation

PHP Associative Array

We can associate a name with each an array elements in PHP using the => symbol.

There are two ways to define an associative array:

1st way:

$salary=array("Adil"=>"350000","Esha"=>"450000","Kartik"=>"200000");

2nd Way:

$salary["Adil"]="350000";  
$salary["Esha"]="450000";  
$salary["Kartik"]="200000";

PHP Example 1-

<?php    
$salary=array("Adil"=>"350000","Esha"=>"450000","Kartik"=>"200000");    
echo "Adil salary: ".$salary["Adil"]."<br/>";  
echo "Esha salary: ".$salary["Esha"]."<br/>";  
echo "Kartik salary: ".$salary["Kartik"]."<br/>";  
?>

Output:

Adil salary: 350000
Esha salary: 450000
Kartik salary: 200000

PHP Example 2-

<?php    
$salary["Adil"]="350000";    
$salary["Esha"]="450000";    
$salary["Kartik"]="200000";    
echo "Adil salary: ".$salary["Adil"]."<br/>";  
echo "Esha salary: ".$salary["Esha"]."<br/>";  
echo "Kartik salary: ".$salary["Kartik"]."<br/>";  
?>

Output:-

Adil salary: 350000
Esha salary: 450000
Kartik salary: 200000

PHP Multidimensional Array

A multi-dimensional array of each element in the main array can also be an array, and each element in the sub-array can be array, and so on. the values in the multidimensional array are accessed using multiple indexes.

An array that contains single or multiple arrays within it and it can be accessed via multiple indexes.

<html>
   <body>
      
      <?php
         $subjectmarks = array( 
            "vijay" => array (
               "physics" => 45,
               "maths" => 38,	
               "chemistry" => 41
            ),
            
            "suresh" => array (
               "physics" => 35,
               "maths" => 48,
               "chemistry" => 45
            ),
            
            "sara" => array (
               "physics" => 48,
               "maths" => 49,
               "chemistry" => 47
            )
         );
         
         /* Accessing multi-dimensional array values */
         echo "Marks for vijay in physics : " ;
         echo $subjectmarks['vijay']['physics'] . "<br />"; 
         
         echo "Marks for suresh in maths : ";
         echo $subjectmarks['suresh']['maths'] . "<br />"; 
         
         echo "Marks for sara in chemistry : " ;
         echo $subjectmarks['sara']['chemistry'] . "<br />"; 
      ?>
   
   </body>
</html>

Output:-

Marks for vijay in physics : 45
Marks for suresh in maths : 48
Marks for sara in chemistry : 47

 

The advantage of PHP Array

Less Code: We don’t need to define multiple variables.
Easy to traverse: With the help of a single loop, we can traverse all the elements of an array.
How to Create PHP Array
Sorting: We can sort the elements of the array.

Resource
PHP.net