Prev | Current Page 140 | Next

W. Jason Gilmore

"Beginning PHP and MySQL: From Novice to Professional"

For example, consider the following string:
CHAPTER 3 ?–  PHP BASICS 67
$color = "maroon";
You could retrieve a particular character of the string by treating the string as an
array, like this:
$parser = $color[2]; // Assigns 'r' to $parser
Compound Datatypes
Compound datatypes allow for multiple items of the same type to be aggregated
under a single representative entity. The array and the object fall into this category.
Array
It??™s often useful to aggregate a series of similar items together, arranging and referencing
them in some specific way. This data structure, known as an array, is formally
defined as an indexed collection of data values. Each member of the array index (also
known as the key) references a corresponding value and can be a simple numerical
reference to the value??™s position in the series, or it could have some direct correlation
to the value. For example, if you were interested in creating a list of U.S. states, you could
use a numerically indexed array, like so:
$state[0] = "Alabama";
$state[1] = "Alaska";
$state[2] = "Arizona";
...
$state[49] = "Wyoming";
But what if the project required correlating U.S. states to their capitals? Rather
than base the keys on a numerical index, you might instead use an associative index,
like this:
$state["Alabama"] = "Montgomery";
$state["Alaska"] = "Juneau";
$state["Arizona"] = "Phoenix";
.


Pages:
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152