Arrays
Line 4: | Line 4: | ||
====Associative Versus Numeric Indexed Arrays==== | ====Associative Versus Numeric Indexed Arrays==== | ||
- | ''Numeric'' arrays use numbers as indexes. PHP will automatically assign a numeric index, starting at 0, when you add an element to a numeric array | + | ''Numeric'' arrays use numbers as indexes. PHP will automatically assign a numeric index, starting at 0, when you add an element to a numeric array -- for example, a numeric array of color words: |
<table> | <table> |
Revision as of 11:34, 15 March 2010
An array is a special kind of variable that lets you store many individual pieces of data. PHP provides many functions for dealing with arrays, such as counting, sorting, and looping through the array data.
Elements are the data values that are stored in the array. Each element can be referenced by its unique array index (or key). The index may be a string or a number.
Contents |
Associative Versus Numeric Indexed Arrays
Numeric arrays use numbers as indexes. PHP will automatically assign a numeric index, starting at 0, when you add an element to a numeric array -- for example, a numeric array of color words:
Index | Element |
---|---|
0 | Black |
1 | Blue |
2 | Red |
3 | Green |
Associative arrays use strings. You must supply an index string when you add elements to an associative array. This example associates household objects with their shapes:
Key | Value |
---|---|
Soda can | Cylinder |
Notepad | Rectangle |
Apple | Sphere |
Orange | Sphere |
An array's elements can be any type of value, including objects and other arrays.
An array's indexes must be scalar (simple, not compound or complex) values such as numbers, strings, or booleans.
Assignment
Array identifiers (names) are followed by square brackets, both to distinguish them from other kinds of variables and to suggest that arrays are indexed. The following code segment assigns Monday and Tuesday to the first two elements of the numeric array named $weekday:
<?php $weekday[] = 'Monday'; // Same as: $weekday[0] = 'Monday'; $weekday[] = 'Tuesday'; // Same as: $weekday[1] = 'Tuesday'; ?>
Note that if you assign indexes yourself, be careful not to skip an index--PHP will NOT report this as an error.
Assignment Using Array
You can also use the array language construct to assign values to an array:
<?php $weekdays = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'); $shapes = array('Soda Can' => 'Cylinder', 'Note Pad' => 'Rectangle', 'Apple' => 'Sphere', 'Orange' => 'Sphere'); ?>
To test whether a variable is an array, use the is_array() function:
<?php $yes = array('this', 'is', 'an', 'array'); echo is_array($yes) ? 'Array' : 'Not an Array'; echo "<br />"; $no = 'This is a string'; echo is_array($no) ? 'Array' : 'Not an Array'; ?>
Looping Through an Array
We can access individual array elements through their indexes either individually or by using a loop:
<?php print "A notepad is a {$shapes['Note pad']}."; // Prints: A Note pad is a Rectangle // The following loop will print all key:value pairs in the $shapes array foreach ($shapes as $key -> $value) { // An associative array has key/value pairs. print "The $key is a $value.<br />"; } ?>
Working With Arrays
<?php $weekdays[] = 'Thursday'; // Adds a value to the end of an existing array $shapes["Megaphone"] = "Cone"; // Adds a value to an existing associative array $numElements = count($shapes); // The count() function returns the number of elements in the array $fruits = array("cherry", "banana", "apple", "orange"); sort($fruits); // The sort() function puts $fruit in alphabetical order foreach($fruits as $key => $value) { echo "fruits[" . $key . "] = " . $value . "<br />"; } ?>
The output from this segment would be:
fruits[0] = apple fruits[1] = banana fruits[2] = cherry fruits[3] = orange
Examples
Here are some array processing examples.