PHP Array tutorial - Multidimensional arrays

Home - Tutorials - Basics

In this tutorial I will show you how to work with arrays in PHP. You will learn how to create, sort or print an array. Besides this we will touch on multidimensional arrays as well.

Tutorial info:

Name:PHP Array tutorial
Total steps:7

Bookmark PHP Array tutorial

Step 5 - Multidimensional arrays

PHP Array tutorial

As each element value of the array can be any type it means that it can be other array as well. If an array element value is an other array then this is a multidimensional array. Of course the internal array values can be arrays as well and so on. You can define 10 dimensional (or more) array if you want.

Creating a multidimensional array is as almost simple as the normal array. Let's see an example:

  1. $myLists['colors'] = array("apple"=>"red",
  2. "grass"=>"green",
  3. "sky"=>"blue",
  4. "night"=>"black",
  5. "wall"=>"white");
  6. $myLists['cars'] = array("BMW"=>"M6",
  7. "Mercedes"=>"E 270 CDI",
  8. "Lexus"=>"IS 220d",
  9. "Mazda"=>"6",
  10. "Toyota"=>"Avensis");
To acces and display an element in the multidimensional array you just extend the key list as follows:
  1. echo $myLists['cars']['Toyota'];

Of course you can define normal, mixed and associative multidimensional arrays.

As last step I will show you some usefull array handling function on the next page.

Previous Step of PHP Array tutorialNext Step of PHP Array tutorial

Tags: array, php array, sort array, create array, print array, array length

Follow phpf1 on Twitter

F1 Site Family
Java F1
JavaScript F1

Total time: 0.1393