Pin Me

A Guide to Linux Programming with PHP - Associative and Multidimensional Arrays

written by: •edited by: Michael Dougherty•updated: 9/2/2009

Arrays don't need to be a boring list of data. It is possible to create an array where the element can have an array of other values as it's own value, which, could hold other arrays also. You could create two or three-dimensional arrays, or more. This article will show you how!

  • slide 1 of 3

    Introduction to Associative Arrays

    In the last article we had a look at some vanilla arrays. This time we'll move on and make things a bit more interesting by getting to grips with associative and multidimensional arrays in PHP.

    Associative arrays differ from normal arrays because associative arrays index the array elements by names given by the user, opposed to numbers.

    With the array() function, you create pairs of keys and values. Below is an example of a simple associative array that stores test results for students:

    <?php

    $Student['Pete'] = 72;

    $Student['Ben'] = 89;

    $Student['Dave'] = 7;

    $Student['Ella'] = 70;

    ?>

    We are creating an array with "Pete", "Ben", "Dave" and "Ella". Then we assign each key with a value, the key "Ben" is assigned the value "89".

    Using the keys we can access any element of the array:

    echo $Student['Ella']; //outputs Ella's score of 70

    Will output "70" in the browser. Instead of repeating ourselves by writing each line over and over, we can write the same array like this:

    <?php

    $Student = array('ScoreArray' => array('Pete' => 72, 'Ben' => 89, 'Dave' => 7, 'Ella' => 70));

    echo $Student['ScoreArray']['Ella']; //outputs Ella's score of 70

    ?>

    See how much better the second attempt is? We have eliminated the need to repeat the name of the array over and over. Writing code to avoid repetition is called the "DRY" programming method. This stands for "Don't Repeat Yourself". It is important that you don't write code unnecessarily as this can degrade performance of your scripts, increase file-size & take up your time! We will talk more about the DRY programming method at a later date.

    Now let's move on to some awesome "multidimensional" arrays!

  • slide 2 of 3

    Multidimensional Arrays

    A multi-dimensional array is an array that is made up of other arrays. I could have an array of cars that contains an array for each car, that tells you the make, model, colour and age, for example.

    To create a multidimensional array we start with a normal array:

    <?php

    $cars = array (

    ); //an empty array

    ?>

    We take that empty array and fill it with an array of cars. In the array of cars we have given the keys values, like so:

    <?php

    $cars = array (

    array ( 'make'=>'MG',

    'model'=>'BGT',

    'colour'=>'Damask Red',

    'year'=>'1978' ),

    array ( 'make'=>'Austin',

    'model'=>'Mini',

    'colour'=>'Harvest Gold',

    'year'=>'1975' ),

    array ( 'make'=>'Jaguar',

    'model'=>'XJS',

    'colour'=>'Carnival Red',

    'year'=>'1987' ),

    );

    ?>

    To access the information in the array, we use the array name $cars, the number of the array that contains the data, for example, MG would be [0], then we write the name of the key e.g. [model].

    To get the age of the Jaguar XJS we write:

    echo $cars[2][age];

    Below, I have summarized what we have just learned and placed the multidimensional array inside some HTML. Remember, you can create an array in one place and use it where ever and whenever you like.

    <html>

    <head>

    <title>PHP Arrays</title>

    </head>

    <body>

    <?php

    $cars = array (

    array ( 'make'=>'MG',

    'model'=>'BGT',

    'color'=>'Damask Red',

    'year'=>'1978' ),

    array ( 'make'=>'Austin',

    'model'=>'Mini',

    'colour'=>'Harvest Gold',

    'year'=>'1975' ),

    array ( 'make'=>'Jaguar',

    'model'=>'XJS',

    'colour'=>'Carnival Red',

    'year'=>'1987' ),

    );

    echo $cars[2][age];

    ?>

    </body>

    </html>

  • slide 3 of 3

    Next Time

    In this article you have learned a fair amount of information about associative and multidimensional arrays in PHP. You have even seen how to create your own and put it to use.

    In the next article in the series we'll learn about PHP operators.

A Guide to Linux Programming with PHP

In this series of articles I'll be teaching you the basics of using PHP, and showing you why I think it's the best language for web development. There are no pre-requisites for your level of knowledge, other than that you can understand basic HTML and are willing to learn.
  1. A Guide to Linux Programming with PHP
  2. A Guide to Linux Programming with PHP - Commenting and Executing Code
  3. A Guide to Linux Programming with PHP - Variables & Data Types
  4. A Guide to Linux Programming with PHP - HTML Forms and PHP
  5. A Guide to Linux Programming with PHP - Arrays
  6. A Guide to Linux Programming with PHP - Associative and Multidimensional Arrays