PHP Arrays
PHP Code Examples
[Download]   [Execute]
<?php include 
// Generate the page header.
$title = "PHP Arrays";
include '../head1.inc';

echo <<<MSG
PHP arrays are remarkably general-purpose data structure.
They
are essentially hash tables, like %hashes in perl, or maps in
Java or C++, but with a number of additional features.
PHP arrays are array-like when the keys are 
contiguous integers.  Keys may be strings or integers, and
values may be of any type, including
other arrays, which allows the programmer to build complex structures.
PHP arrays also retain keys in the order in which they are 
first inserted, allowing arrays to function as lists also.
<p>
MSG;

echo "Here's a hash-like array: ";
$bill = array("snake" => "oil", "tv" => "wasteland", "titanic" => "sank");
print_r($bill); print "<p>";

echo "Here's an array-like array: ";
$fred = array(0 => "oil", 1 => "wasteland", 2 => "sank");
print_r($fred); print "<p>";

echo "Array-like arrays can be created with a list syntax: ";
$alice = array("oil", "wasteland", "sank");
print_r($alice); print "<p>";

echo "Use [] to subscript.<br>";
$arr = array(5, 10, 34, 11, 2);
print_r($arr); echo "<br>";
$arr[1] = "Mike";
$arr[5] = 3.3;
$arr["zippy"] = "...";
$arr[33] = "Tuesday";
$arr[10] = 22;
print_r($arr); print "<p>";

echo "Default subscript is 1 + max existing numberic subscript, or zero.<br>";
$z = array(5 => 3, 19, 87, 1, 77);
print_r($z); print "<br>";
$p = array(3, 11, 20 => "snakes", "horses", "antelope", 10 => 4.4, "Smile");
print_r($p); print "<br>";
?>
</body>
</html>