The PHP array (as well as arrays in other programming/scripting languages) is a special type of data structure that allows the programmer to save multiple similar variables under the same name. This is useful since it allows the programmer to perform the same task(s) on all of the similar variables at once.
The use of arrays to group similar variables makes manipulating these variables much easier. The PHP array tutorial below will demonstrate.
A numeric array is a data structure that stores similar variables together in one location and uses a number to record the address of each variable.
Let's say we have a list of songs, we could store it like this:
$song0 = 'Tonight will be forever'; $song1 = 'I will always love you'; $song2 = 'Don\'t turn off the music';
However, it would be better and more efficient to store them in an array like below:
$song = 'Tonight will be forever'; $song = 'I will always love you'; $song = 'Don\'t turn off the music';
Numeric arrays take the form:
$array_name[$index] = value;
The variable $index is a numeric identifier which tells the position of each variable in the array.
Numeric arrays in PHP are useful because they will allow us to manipulate all our similar variables with less code. If we want to perform a task that involves the values stored in an array we can do it very simply as shown in the example below.
<?php $song = 'Tonight will be forever'; $song = 'I will always love you'; $song = 'Don\'t turn off the music'; for ($i = 0; $i < 3; $i++) echo $song[$i] . '<br />'; ?>
The result would be:
We were able to echo all 3 variables with 2 lines of code. We simply placed the echo command inside of a for loop which then iterated through all the items in the array. Imagine how much time this would save if our array size was 100 instead of 3.
We usually would not want to hardcode the size of the array in our loop as we did above (with the number 3). A better approach would be to use a function to determine the PHP array size. That way it would not matter what the size of the array was. If we used a bigger array if would automatically know to loop to the end of the array. This makes our code more robust and extensible.
A PHP associative array is slightly different. In an associative array, a key is associated with a value and not a number. For example, suppose we wanted to store the prices of various meals we would do this:
<?php $prices['Chicken'] = '$5'; $prices['Shrimp'] = '$7'; $prices['Fish'] = '$3'; echo 'The price of chicken is ' . $prices['Chicken']; ?>
By using an associative array, we would have meaningful names with which to identify our variables when we are storing or retrieving them from the array.
The key to mastering arrays is using for loops and while loops to iterate through the items they contain so that you are able to work on the many variables that they may contain at once.
PHP arrays are powerful data structures that have the potential of making our life as the programmer easier. Remember: if you have many similar variables, store them as an array; it will make your life easier.
We hope this PHP array tutorial was useful.