PHP Array Values


OK, so you now know how to store values in your array. But how do you get at those values? Well, there are few ways you can do it. But the "Key" is the key. Here's an example for you to try:


$seasons = array("Autumn", "Winter", "Spring", "Summer");
print $seasons[0];


The array is the same one we set up before. To get at what is inside of an array, just type the key number you want to access. In the above code, we're printing out what is held in the 0 position (Key) in the array. You just type the key number between the square brackets of your array name:

print $Array_Name[0];

You can also assign this value to another variable:

$key_data = $Array_Name[0];
print $key_data;

It's a lot easier using a loop, though. Suppose you wanted to print out all the values in your array. You could do it like this:

$seasons = array("Autumn", "Winter", "Spring", "Summer");

print $seasons[0];
print $seasons[1];
print $seasons[2];
print $seasons[3];

Or you could do it like this:

for ($key_Number = 0; $key_Number < 4; $key_Number++) {

print $seasons[$key_Number];


If you have many array values to access, then using a loop like the one above will save you a lot of work!

You don't have to use numbers for the keys - you can use text. We'll see how to do that in the next part.