-
Step 1
The first step is to how you define an array. To define an array you can use the following:
$statenames=array('NJ','PA','NY','CA','TX')
This line of code says that the variable $statenames is an array (as noted by the keyowrd "array") and that we are assigning values to it. If you want to print out a specific item in this array you could write:
echo $statenames[0]
This would print 'NJ'. The zero is the index of the array and all arrays always start at zero up through the total number of items in the array minus 1. In our example here, we have 5 items in the array, therefore the indexs go from 0 through 4. -
Step 2
Instead of indexes you can use key value pairs. Our above array can be written as:
$statenames=array('mystate'=>'NJ','neighbor'=>'PA','bigapple'=>'NY')
If you want to retrieve the value of 'bigapple' you would write:
echo $statenames['bigapple']
That would display 'NY'. -
Step 3
To modify an element within an array you can do the following for the first step:
$statenames[0]='NH'
This changes element zero from NJ to NH
For our example in step 2 you would write:
$statenames['neighbor']='DE'
This changes the value from PA to DE.









Comments
tomglander said
on 7/19/2009 Well, now I understand better what an array is. Good explanation. Reminds me of programming class, except better explanations. Good work!