Multidimensional Arrays PHP

An array element can be any type of data. You've seen numbers and strings, but you can even put an array inside an array. An array of arrays is also called a multidimensional array. Imagine a ten-by-ten grid. You've got 100 different squares, each of which can have its own value. One way to represent this in code is a two-dimensional array: a tenelement array of ten-number arrays, ten rows of ten columns.

To reference a single element, you first use square brackets to pick the first dimension (row), then use a second pair of brackets to pick the second dimension (column). Row 3, column 7, would be written as $someArray[3][7]. initializes a multidimensional array using the array function. This shows that multidimensional arrays are just arrays of arrays.

Creating and Referencing a Multidimensional Array

<?
$Cities = array(
"California"=>array(
"Martinez",
"San Francisco",
"Los Angeles"
),
"New York"=>array(
"New York",
"Buffalo"
)
print($Cities["California"][1]);
?>

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics