January 15, 2009

Understanding Multidimensional Arrays

An array is a list of values, a data structure made up of a group of elements.

Like appearing out of the fog, arrays became more clear to me only after using them for data storage and transportation in a few programs.  Arrays get a little tricky especially when you start working with the multidimensional kind.

This is the simplest form of arrays, a list of elements.

//a one dimensional array
$fruit = array(apples, bananas, oranges, pears);

Essentially a multidimensional array is an array where the elements of that array are also arrays.  Arrays within arrays.

A two dimensional array would be one array with multiple arrays in it (as it’s elements).  This could be visually represented as a single table of columns and rows.

//a two dimensional array
$fruit = array(
    array(braeburn, granny smith, fuji, gala, golden delicious),
    array(cavendish, ecuadorian dwarf, manzano, lady fingers),
    array(blood, navel, valencia, pineapple, queen),
    array(yellow bartlett, red bartlett, bosc, comice)
);

A three dimensional array would be one array with multiple arrays in it with multiple arrays contained in each one of those.

//a three dimensional array
$foods = array(
    fruits=>array(
        array(braeburn, granny smith, fuji, gala, golden delicious),
        array(cavendish, ecuadorian dwarf, manzano, lady fingers),
        array(blood, navel, valencia, pineapple, queen),
        array(yellow bartlett, red bartlett, bosc, comice)
    ),
    vegetables=>array(
        array(butternut, crookneck, scallop, pattypan),
        array(gherkin, cornichon, garden cucumber, english cucumber),
        array(iceberg, romaine, batavian)
    )
);

There is functionally no limit to the depth of arrays (except your system memory) you can have 4 5 and 6 dimensions (or more) but their use may not be expedient or may unnecessarily complicate things.

Here are a few things that maybe you didn’t know about arrays.

A common task is to take an two dimensional array (such as a mysql query result) and convert it into a tabular display.

$results = mysql_query($sql, $link);</code>

foreach ($results as $row) {
    echo "<tr>";
    foreach ($row as $value) {
        echo "<td>;" . $value ."</td>";
    }
    echo "</tr>";
}

If you want to apply your array into a session variable, such as for a shopping cart, you can use serialize() to put your array into a storable representation of the array, and you can use unserialize() on the other side to put it back into a standard array type variable.

//the output of a serialized array...
a:4:{i:0;a:5:{i:0;s:8:"braeburn";i:1;s:12:"granny smith";i:2;s:4:"fuji";i:3;s:4:"gala";i:4;s:16:"golden delicious";}i:1;a:4:{i:0;s:9:"cavendish";i:1;s:16:"ecuadorian dwarf";i:2;s:7:"manzano";i:3;s:12:"lady fingers";}i:2;a:5:{i:0;s:5:"blood";i:1;s:5:"navel";i:2;s:8:"valencia";i:3;s:9:"pineapple";i:4;s:5:"queen";}i:3;a:4:{i:0;s:15:"yellow bartlett";i:1;s:12:"red bartlett";i:2;s:4:"bosc";i:3;s:6:"comice";}}

Learning about arrays will be worth your time, it will get you thinking about more complex data structures and how to handle them more efficiently. I encourage you to start experimenting with arrays today!

Leave a comment