Multidimensional Arrays

A simple design feature in PHP’s array syntax makes it possible to create arrays of more than one dimension. In fact, they can be as many dimensions as you like (although it’s a rare application that goes further than three).
And that feature is the ability to include an entire array as a part of another one, and to be able to keep on doing so, just like the old rhyme: “Big fleas have little fleas upon their backs to bite ’em. Little fleas have lesser fleas, add flea, ad infinitum.” Let’s look at how this works by taking the associative array in the previous example and extending it.

$products = array(
		'paper' => array(
				'copier' => "Copier & Multipurpose",
				'inkjet' => "Inkjet Printer",
				'laser' => "Laser Printer",
				'photo' => "Photographic Paper"),
		'pens' => array(
				'ball' => "Ball Point",
				'hilite' => "Highlighters",
				'marker' => "Markers"),
		'misc' => array(
				'tape' => "Sticky Tape",
				'glue' => "Adhesives",
				'clips' => "Paperclips") );
echo "<pre>";
foreach ($products as $section => $items)
	foreach ($items as $key => $value)
	echo "$section:\t$key\t($value)<br>";
echo "</pre>";

Output :

paper:	copier	(Copier & Multipurpose)
paper:	inkjet	(Inkjet Printer)
paper:	laser	(Laser Printer)
paper:	photo	(Photographic Paper)
pens:	ball	(Ball Point)
pens:	hilite	(Highlighters)
pens:	marker	(Markers)
misc:	tape	(Sticky Tape)
misc:	glue	(Adhesives)
misc:	clips	(Paperclips)

You may also like...