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).
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âsee ExampleÂ 6-10.
<?php $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>"; ?>
To make things clearer now that the code is starting to grow, Iâve
renamed some of the elements. For example, seeing as the previous array
$paper is now just a subsection of a
larger array, the main array is now called
$products. Within this array there are three
misc, and ...
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.