Achieve the World’s Most Prestigious Certifications and Unlock Top-Paying Careers! Get Certified in the Industry’s Leading Programs Today.
🚀 DevOps Certified Professional 🚀 SRE Certified Professional 🚀 DevSecOps Certified Professional 🚀 MLOps Certified Professional
📅 Starting: 1st of Every Month 🤝 +91 8409492687 | 🤝 +1 (469) 756-6329 🔍 Contact@DevOpsSchool.com

PHP | Multidimensional arrays

PHP
Pascal - Multi-dimensional Array

Multi-dimensional arrays is a type of arrays which stores an another array at each index instead of single element. In simple words, multi-dimensional arrays as array of arrays, 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 dimensions.
Dimensions of multidimensional array indicates the number of indices needed to select an element. For a two dimensional array two indices to select an element.

Associative array: Al associative array is similar to indexed array but instead of linear storage (indexed storage), every value can be assigned with a user-defined key of string type. Example :-

//Associative array is kind of array which is used to define keys at the place of index of aary.
//Associative array example
$newArray1 = [
"new1" => "manishkumar",
"new2" => "amitkumar", //--> This is Value and left side is "Keys"
"new3" => 19,
"new5" => 90,
"new6" => True,
"new7" => False,
"new8" => NULL
];
echo "<pre>";
print_r($newArray1);
echo "</pre>";
echo "<br><br><br>";
echo $newArray1["new3"] . "<br>";
echo $newArray1["new5"] . "<br>";
echo $newArray1["new6"] . "<br>";
echo $newArray1["new7"] . "<br>";
echo $newArray1["new8"] . "<br>";
view raw example1.php hosted with ❤ by GitHub

Output :-

Array
(
    [new1] => manishkumar
    [new2] => amitkumar
    [new3] => 19
    [new5] => 90
    [new6] => 1
    [new7] => 
    [new8] => 
)

19
90
1

Multidimensional Array : It is the form of multidimensional array. Initialization in Three-Dimensional array is same as that of Two-dimensional arrays. The difference is as the number of dimension increases so the number of nested braces will also increase.

Accessing multidimensional array elements: There are mainly two ways to access multidimensional array elements in PHP.

  • Elements can be accessed using dimensions as array_name[‘first dimension’][‘second dimension’].
  • Elements can be accessed using for loop.
  • Elements can be accessed using for each loop.
<?php
//Multi - Dimensional Array.
$newArray = [
[1, "Mukesh", "Manager", "Digital Marketing", 50000],
[2, "Nitesh", "Asst Manager", "Software Develoer", 35000],
[3, "Nilotma", "Senior Manager", "Tech team", 76000],
[4, "ritesh", "Junior Manager", "SEO", 15000],
[5, "Rakesh", "Area Manager", "Sales Marketing", 40000]
];
//Hera we gonna put all this data in tabular Form with the help of FOR Loop.
for($i = 0;$i < 5;$i++){
for($j = 0;$j < 5;$j++){
echo $newArray[$i][$j] . " ";
}
echo "<br>"; //After each rotation it will take a break line.
}
echo "<br><br><hr>";
//We can also make this in Table form.
echo "<table border = '2px' cellpadding = '5px' cellspacing = '0'>"; // using TABLE in html.
echo "<tr>;
<th>Emp Id</th>
<th>Emp Name</th>
<th>Emp Designation</th>
<th>Emp Department</th>
<th>Emp Salary</th>
</tr>";
for($i = 0;$i < 5;$i++){
echo "<tr>";
for($j = 0;$j < 5;$j++){
echo "<td>" . $newArray[$i][$j] . "</td>";
}
echo "</tr>"; //After each rotation it will take a break line.
}
echo "</table>";
echo "<br><br><hr>";
//Same Multi-dimensional Array we can write in FOR Each loop too.
//lets take a example.
$newArray1 = [
[1, "Manish", "Manager", "Area Manager", 73000],
[2, "Kumar", "Senior Manager", "Tech team", 65000],
[3, "Kumari Puja", "Junior Manager", "Technical deptt", 66000],
[4, "Nillu Kumari", "Asst Manager", "Software Devloper", 35000],
[5, "Sita prasad", "Software Develoer", "Sales Area Manager", 48000]
];
//we can also write it in Table Format.
echo "<table border = '1px' cellpadding = '5px' cellspacing = '0'>";
echo "<tr>;
<th>Emp Id</th>
<th>Emp Name</th>
<th>Emp Designation</th>
<th>Emp Department</th>
<th>Emp Salary</th>
</tr>";
foreach($newArray1 as $value1){
echo "<tr>";
foreach($value1 as $value2){
echo "<td>$value2</td>";
}
echo "<tr>";
}
echo "</table>";
?>
view raw example2.php hosted with ❤ by GitHub

Output:-

1 Mukesh Manager Digital Marketing 50000
2 Nitesh Asst Manager Software Develoer 35000
3 Nilotma Senior Manager Tech team 76000
4 ritesh Junior Manager SEO 15000
5 Rakesh Area Manager Sales Marketing 40000


Emp IdEmp NameEmp DesignationEmp DepartmentEmp Salary
1MukeshManagerDigital Marketing50000
2NiteshAsst ManagerSoftware Develoer35000
3NilotmaSenior ManagerTech team76000
4riteshJunior ManagerSEO15000
5RakeshArea ManagerSales Marketing40000

;

Emp IdEmp NameEmp DesignationEmp DepartmentEmp Salary
1ManishManagerArea Manager73000
2KumarSenior ManagerTech team65000
3Kumari PujaJunior ManagerTechnical deptt66000
4Nillu KumariAsst ManagerSoftware Devloper35000
5Sita prasadSoftware DeveloerSales Area Manager48000

Copy code and use it by yourself on your editor for better understanding of Output.

Thank You !!