PHP MySQL Tutorial-8- Multidimensional Arrays Part 2

  • 6 years ago
In PHP, two-dimensional arrays are arrays that contain arrays/ array with in array. You can think of the outer array as containing the rows and the inner arrays as containing the data cells in those rows. For example, a two-dimensional array called $foods could contain the names of the HEALTHY and UNHEALTHY foods. Below is a grid that represents such a two-dimensional array.

Healthy Foods: Unhealthy Foods:
Salad Ice Cream
Vegetables Pizza
Pasta


To read an element from a two-dimensional array, you must first identify the index of the "row" and then identify the index of the "column." For example, the song "Angie" is in row 1, column 2, so it is identified as $foods[‘Healthy’][1]. Or $foods[‘Healthy’][2].
Remember that the first row is row 0 and the first column is column 0.

For accessing all elements of multidimensional array, we use for each construct.