PHP Arrays

by Hiroshi on June 23, 2008

in Arrays, PHP Basics

An array is a data structure that stores one or more values in a single value. In more detail ‘An array’ in PHP is actually an ordered map. A map is a type that maps values to keys. This type is optimized in several ways, so you can use it as a real array, or a list (vector), hashtable (which is an implementation of a map), dictionary, collection, stack, queue and probably more. Because you can have another PHP array as a value, you can also quite easily simulate trees.

An array can be created by the array() language-construct. It takes a certain number of comma-separated key => value pairs.

$my_array = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $my_array["somearray"][6];    // Output: 5
echo $my_array["somearray"][13];   // Output: 9
echo $my_array["somearray"]["a"];  // Output: 42
$programming_array[0] = "PHP";
$programming_array[1] = "ASP";
$programming_array[2] = "AJAX";
$programming_array[3] = "JSP";
echo "Two of most valuable programming resources are " . $programming_array[0] . " & " . $programming_array[2];
// Output:
// Two of most valuable programming resources are PHP & AJAX

Related Posts

Previous post:

Next post: