PHP Multidimensional Arrays

A multidimensional array in PHP is an array containing one or more arrays. Multidimensional arrays allow you to create complex data structures, such as matrices, tables, or any nested structure. PHP supports both indexed and associative arrays as sub-arrays in a multidimensional array.

Note

The dimension of an array indicates the number of indices required to access a specific element in that array.

  • You need 2 indices to access an element of two-dimensional array
  • You need 3 indices to access an element of three-dimensional array

PHP Multidimensional Array Example


Let's see a simple example of PHP multidimensional array to display following tabular data. In this example, we are displaying 3 rows and 2 columns.


Name

Age

John

30

Alice

25

Bob

35

To get access to the elements of the above table we must point to the two indices (row and column):

Example
<!DOCTYPE html>
<html>
<body>
<?php
$multiArray = [
["name" => "John", "age" => 30],
["name" => "Alice", "age" => 25],
["name" => "Bob", "age" => 35]
];

// Accessing elements
echo $multiArray[1]["name"]; // Output: Alice
echo $multiArray[0]["age"]; // Output: 30
?>
</body>
</html>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.