Arrays are very useful for storing multiple pieces of information in a single variable. PHP has many built in functions that allow you to store, manage and manipulate arrays in various ways. What exactly is an array, anyway?
An array is basically an arrangement of data. You can use them to store all sorts of information, for instance, I use arrays for this website to store the data for the poll you see in the left column. I also use them to store the information of the tutorials on this site... titles, descriptions, views, etc.
To create an array, all you need to do is call upon the array() function.
An example array is shown below.
<?php
|
|
The code above creates an array with the values between the parenthesis, each piece separated by commas becomes a new item in the array. You can also add items to an already existing array, here is an example how:
<?php
|
|
PHP will automatically add a new array item containing 'pears' to the end of the array.
So now the array will look like this:
Array
|
|
Each item in the array has what is called a 'key'. As you can see in the array above the keys are numbers, starting from 0 the keys increment by 1. For example, the key for the value 'apple' is 0, the key for 'banana' is 1, and so on.
The keys of the array items are important when you want to grab the values
of the items in the array. To do this, you use this syntax:
$value = $array_name[key_number];
Here is an example:
<?php
|
|
The above code will write 'apples are my favorite fruit.' onto the webpage.
We're not done yet, go to the next page to learn more.
| Discuss Tutorial: Utilizing Arrays | 5 Comments |

Nice tutorial
