PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
PHP allows for the creation of multidimensional arrays, or arrays of arrays. These can be useful for a variety of purposes, such as organizing related data sets or representing more complex data structures like tables or matrices.
Two-Dimensional Arrays
A two-dimensional array is essentially an array of arrays. Here's an example:
$matrix = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) );
In this example, $matrix
is an array that contains three arrays. Each of these inner arrays contains three elements. To access elements in a two-dimensional array, you use two indices. The first index identifies the inner array, and the second index identifies the element within that array.
For example, to access the number 5 in the $matrix
array, you would do this:
echo $matrix[1][1]; // Outputs: 5
Multidimensional Arrays
You can also have arrays with more than two dimensions. These are called multidimensional arrays. For example, here is a three-dimensional array:
$threeD = array( array( array(1, 2, 3), array(4, 5, 6) ), array( array(7, 8, 9), array(10, 11, 12) ) );
In this case, $threeD
is an array that contains two arrays, each of which also contains two arrays.
To access elements in a three-dimensional array, you use three indices. For example, to access the number 8, you would do this:
echo $threeD[1][0][1]; // Outputs: 8
Looping Through Multidimensional Arrays
Looping through a multidimensional array typically involves nested loops. Here's how you could loop through the $matrix
array:
foreach ($matrix as $row) { foreach ($row as $element) { echo $element . " "; } echo "\n"; }
This script would output:
1 2 3 4 5 6 7 8 9
Each outer loop iteration corresponds to one row of the matrix, and the inner loop iterates over the elements in each row.
PHP Multidimensional Array Example:
$matrix = array( array(1, 2, 3), array(4, 5, 6), array(7, 8, 9) );
PHP 2D Array Initialization:
$matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ];
PHP array_push for Multidimensional Arrays:
array_push
is used to add one or more elements to the end of an array. For a multidimensional array, it can be used to add a new row.$newRow = [10, 11, 12]; array_push($matrix, $newRow);
PHP Access Element in 2D Array:
$element = $matrix[1][2]; // Accesses the element at row 1, column 2 (value: 6)
PHP Foreach Loop for Multidimensional Arrays:
foreach
loop is used to iterate over elements in an array. For a 2D array, it can be used to traverse rows and columns.foreach ($matrix as $row) { foreach ($row as $value) { echo $value . ' '; } echo '<br>'; }
PHP array_merge for Multidimensional Arrays:
array_merge
is used to merge two or more arrays. For multidimensional arrays, it can merge rows.$array1 = [1, 2, 3]; $array2 = [4, 5, 6]; $mergedArray = array_merge($array1, $array2);
PHP Sort Multidimensional Array:
usort
.usort($matrix, function($a, $b) { return $a[0] - $b[0]; // Sort based on values in the first column });
PHP Search in Multidimensional Array:
$searchValue = 5; foreach ($matrix as $row) { if (in_array($searchValue, $row)) { echo 'Value found!'; break; } }
PHP Transpose 2D Array:
$transposedMatrix = array_map(null, ...$matrix);