3 Types of Arrays in PHP with Examples

In this article we are going to cover Arrays in PHP with Examples, What is Array in PHP, Advantage of PHP Array, Types of Array in PHP.

What is Arrays in PHP ?

An array stores multiple values in one single variable. The arrays are helpful to create a list of elements of similar types, which can be accessed using their index or key. An array is created using an array() function in PHP.

Advantage of PHP Array

Less Code: we don’t need to define multiple variables.

Easy to traverse: we can traverse all the elements of an array.

Sorting: We can sort the elements of array.

Types of Arrays in PHP

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

#1. Indexed Arrays in PHP :

In PHP, these type of arrays can be used to store any type of elements, but an index is always a number. All elements are represented by an index number which start from 0.

There are two ways to define indexed array:

The index can be assigned automatically (index always starts at 0), like this:

$bike = array(“Royal Enfield”, “KTM”, “YZ”);

OR the index can be assigned manually:

$bike[0] = “Royal Enfield”;
$bike[1] = “KTM”;
$bike[2] = “YZ”;

Example of first way indexed Array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$bike = array("Royal Enfield", "KTM", "YZ"); 
echo "I like " . $bike[0] . ", " . $bike[1] . " and " . $bike[2] . ".";
?>

</body>
</html>

Output:

I like Royal Enfield, KTM and YZ.
3 Types of Arrays in PHP with Examples 1

Example of second way indexed Array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$bike[0] = "Royal Enfield";
$bike[1] =   "KTM";
$bike[2] =    "YZ"; 
    
echo "I like " . $bike[0] . ", " . $bike[1] . " and " . $bike[2] . ".";
?>

</body>
</html>

Output:

I like Royal Enfield, KTM and YZ.
3 Types of Arrays in PHP with Examples 2

Traversing Indexed Arrays in PHP:

We can easily traverse array in PHP using foreach loop.

Example of traversing indexed array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php  
$bike = array("Royal Enfield", "KTM", "YZ"); 
foreach( $bike as $bike1 )  
{  
  echo "I like $bike1<br />";  
}  
?>  

</body>
</html>

Output:

I like Royal Enfield
I like KTM
I like YZ
3 Types of Arrays in PHP with Examples 3

#2. Associative Arrays in PHP:

In PHP, these types of arrays are similar to the indexed arrays but instead of linear storage, every value can be assigned with a user-defined key of string type.

These are two ways to create an associative array:

$mark = array(“Ajay” => “80”, “Vijay” => “75”, “Sanjay” => “90”);

Or

$mark[‘Ajay’] = ‘80’;
$mark[‘Vijay’] = ‘75’;
$marks[‘Sanjay’] = ‘90’;

Example of first way Associative Array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$marks = array("Ajay"=>"80", "Vijay"=>"75", "Sanjay"=>"90");
echo "Vijay got " . $marks['Vijay'] . " marks.";
?>

</body>
</html>

Output:

Vijay got 75 marks.
3 Types of Arrays in PHP with Examples 4

Example of second way Associative Array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$marks = array("Ajay"=>"80", "Vijay"=>"75", "Sanjay"=>"90");
echo "Vijay got " . $marks['Vijay'] . " marks. <br>"; 
echo "Ajay got " . $marks['Ajay'] . " marks. <br>"; 
echo "Sanjay got " . $marks['Sanjay'] . " marks.";
?>

</body>
</html>

Output:

Vijay got 75 marks.
Ajay got 80 marks.
Sanjay got 90 marks.
3 Types of Arrays in PHP with Examples 5

Traversing Associative Arrays in PHP:

We can easily traverse array in PHP using foreach loop.

Example of traversing Associative array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$marks = array("Ajay"=>"80", "Vijay"=>"75", "Sanjay"=>"90");
foreach($marks as $k => $v) {  
echo "Key: ".$k." Value: ".$v."<br/>";  
}
?>

</body>
</html>

Output:

Key: Ajay Value: 80
Key: Vijay Value: 75
Key: Sanjay Value: 90
3 Types of Arrays in PHP with Examples 6

#3. Multidimensional Arrays in PHP:

Sometimes we want to store values with more than one key. For this we have multidimensional arrays.

A multidimensional arrays is an array containing one or more arrays. In multidimensional array every element in this array can be an array and they can also hold other sub-arrays within. Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensional.

Example of Multidimensional Array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$stud = array (
  array("Vijay",14,90),
  array("Ajay",14,86),
  array("Snjay",14,87)
);
  
echo $stud[0][0].": Age: ".$stud[0][1].", Marks: ".$stud[0][2].".<br>";
echo $stud[1][0].": Age: ".$stud[1][1].", Marks: ".$stud[1][2].".<br>";
echo $stud[2][0].": Age: ".$stud[2][1].", Marks: ".$stud[2][2].".<br>";
?>

</body>
</html>

Output:

Vijay: Age: 14, Marks: 90.
Ajay: Age: 14, Marks: 86.
Snjay: Age: 14, Marks: 87.
3 Types of Arrays in PHP with Examples 7

Another Example of Multidimensional Array in PHP:

<!DOCTYPE html>
<html>
<body>

<?php
$stud = array (
  array("Vijay",14,90),
  array("Ajay",14,86),
  array("Snjay",14,87)
);
  
for ($row = 0; $row < 3; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$stud[$row][$col]."</li>";
  }
  echo "</ul>";
}
?>

</body>
</html>


Output:

Row number 0
•	Vijay
•	14
•	90
Row number 1
•	Ajay
•	14
•	86
Row number 2	
•	Snjay
•	14
•	87
3 Types of Arrays in PHP with Examples 8

Conclusion:

We have covered Arrays in PHP with Examples, What is Array in PHP, Advantage of PHP Array, Types of Array in PHP.

Related Articles:

Reference:

Arrays in PHP official page

Shweta Mamidwar

I am Shweta Mamidwar working as a Intern in Product Company. Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap