⊗ppPmMdInr 132 of 447 menu

Multidimensional Arrays in PHP

Array elements can be not only strings and numbers, but also arrays. In this case, we get an array of arrays or a multidimensional array. In the following example, the array $arr consists of three elements, which in turn are arrays:

<?php $arr = [['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]; ?>

Let's rewrite it in a more understandable form:

<?php $arr = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ]; ?>

Depending on the nesting level, arrays can be two-dimensional - an array of arrays, three-dimensional - an array of arrays of arrays (and so on - four-dimensional, five-dimensional, etc.).

The array above is two-dimensional, since inside one array there are other subarrays and these subarrays do not contain other arrays. To output any element from a two-dimensional array, you should write not one pair of square brackets, but two:

<?php $arr = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ]; echo $arr[0][1]; // outputs 'b' echo $arr[1][2]; // outputs 'f' ?>

Given the following array:

<?php $arr = [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'], ['j', 'k', 'l'], ]; ?>

Using it, output the elements with the text 'l', 'e', 'g' and 'a'.

Given the following array:

<?php $arr = [[1, 2], [3, 4], [5, 6]]; ?>

Find the sum of all its elements.

byenru