<?php $paper['copier'] = "Copier & Multipurpose"; $paper['inkjet'] = "Inkjet Printer"; $paper['laser'] = "Laser Printer"; $paper['photo'] = "Photographic Paper"; echo $paper['laser']; ?>
<?php $p1 = array("Copier", "Inkjet", "Laser", "Photo"); echo "p1 element: " . $p1[2] . "<br>"; $p2 = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); echo "p2 element: " . $p2['inkjet'] . "<br>"; ?>
<?php $paper = array("Copier", "Inkjet", "Laser", "Photo"); $j = 0; foreach($paper as $item) { echo "$j: $item<br>"; ++$j; } ?>
<?php $paper = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); foreach($paper as $item => $description) echo "$item: $description<br>"; ?>
<?php $paper = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); while (list($item, $description) = each($paper)) echo "$item: $description<br>"; ?>
<?php list($a, $b) = array('Alice', 'Bob'); echo "a=$a b=$b"; ?>
<?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: $key ($value)<br>"; echo "</pre>"; ?>
<?php // This example is corrected from the one in the book, // which had two extra blank rows in the center. $chessboard = array( array('r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'), array('p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'), array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '), array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '), array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '), array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '), array('P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'), array('R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R') ); echo "<pre>"; foreach($chessboard as $row) { foreach ($row as $piece) echo "$piece "; echo "<br>"; } echo "</pre>"; ?>
<?php $temp = explode(' ', "This is a sentence with seven words"); print_r($temp); ?>
<?php $temp = explode('***', "A***sentence***with***asterisks"); print_r($temp); ?>
<?php $fname = "Doctor"; $sname = "Who"; $planet = "Gallifrey"; $system = "Gridlock"; $constellation = "Kasterborous"; $contact = compact('fname', 'sname', 'planet', 'system', 'constellation'); print_r($contact); ?>
<?php $j = 23; $temp = "Hello"; $address = "1 Old Street"; $age = 61; print_r(compact(explode(' ', 'j temp address age'))); ?>
<?php $paper[] = "Copier"; $paper[] = "Inkjet"; $paper[] = "Laser"; $paper[] = "Photo"; print_r($paper); ?>
<?php $paper[0] = "Copier"; $paper[1] = "Inkjet"; $paper[2] = "Laser"; $paper[3] = "Photo"; print_r($paper); ?>
<?php $paper[] = "Copier"; $paper[] = "Inkjet"; $paper[] = "Laser"; $paper[] = "Photo"; for ($j = 0 ; $j < 4 ; ++$j) echo "$j: $paper[$j]<br>"; ?>