In this lecture we'll explore p p arrays.
We've already had a bit of exposure to arrays in some of the previous lessons an array is a variable
that can store multiple values in BHP.
There are three types of arrays indexed arrays associative arrays and Multi-dimensional arrays we'll
start with an example of an index to re.
Here we have a variable named colors.
The variable contains three values blue green and red we can output each value in the array by specifying
its unique index number in square brackets is seen here.
We have an echo statement and then just some text.
My favorite color my favorite colors are and then each each value in the array is referenced using its
index number in square brackets an index always starts with the number zero.
So the zero in this case is assigned to the first value in the array glue would have the index number
zero the index number increments by 1.
So the value Green would have the index number of 1 and red would have the value or the index numbers
are u of 2 we can see the output in our web browser HERE
WE CAN USE THE COUNT function to return the length of the number of values in an array.
In this case we have three values blue green and red by typing count and then the variable name colors.
We can see that three is output in our web browser
we can loop through the values of an index story using the for the first we specify we want the value
of x to start at 0
and increment by 1
up to the length of the array and the length of the array is obtained using our count function and storing
the value in a variable called our length
so we can see that variable here as well.
Next we simply loop through each index number in our array which will output the value attached to the
index number and that's done here echo colors.
And then the index number and we can see the output here in our web browser.
The next type of array we want to explore is the associative array this type of an array allows us to
assign values to our elements.
And this is referred to as named keys in this example.
We've created an associate of array variable named T score
for the purpose of this example.
We'll assume t score holds the mark that students scored on a test out of one hundred Peace Corps has
three elements.
John Bill and Dan Each element has a named key or value assigned to it.
So John is assigned 60.
Bill is assigned 80 and then is assigned 75 next.
We use our echo statement to output the mark that Bill got on the test we do that using our echo statement
then inserting the named key in square brackets next to the Associated array name is seen here and in
our web browser we can see the corresponding OutPut.
Bill scored 80 out of 100
we can loop through an associate of array using the for each loop and we've gone over the for each loop
before.
So we can see the output displayed in our web browser but this time with the named key value as well.