PHP 5 Multidimensional Arrays

Array does not have to be a simple list of keys and values; each array element can contain another array as a value, which in turn can hold other arrays as well. In such a way you can create two-dimensional or three-dimensional array.

  • Two-dimensional Arrays
  • Three-dimensional Arrays
  • Two-dimensional Arrays

    Imagine that you are an owner of a flower shop. One-dimensional array is enough to keep titles and prices. But if you need to keep more than one item of each type you need to use something different - one of the ways to do it is using multidimensional arrays. The table below might represent our two-dimensional array.

    Example -

    Three-dimensional Arrays

    You don’t have to be limited by two dimensions: the same way as array elements can contain other arrays, these arrays, in their turn, can contain new arrays.Three-dimensional array is characterized by height, width, and depth. If you feel comfortable to imagine two-dimensional array as a table, then imagine a pile of such tables. Each element can be referenced by its layer, row, and column.

    If we classify flowers in our shop into categories, then we can keep data on them using three-dimensional array. We can see from the code below, that three-dimensional array is an array containing array of arrays:

    Example -