
This week we’ll be going over arrays and foreach loops. Both are very powerful and useful, and will be used frequently in your programming.
Arrays
No, arrays are not loops, however we’ll be taking this slight detour as foreach loops are based around arrays. An array is a type, just as variables, strings and integers are all types. Arrays can be best described as an ordered map. They can either associate values to keys, or be a list of values. Arrays are often contained within a variables. Let’s look at a couple of value and key based arrays:
[code='php']$array1 = array('one' => 1,
'two' => 2,
'three' => 3);
$array2 = array('first_name' => 'Matt',
'last_name' => 'Freedman',
'website' => 'http://mattsblog.ca/');[/code]
Arrays syntax are simple, keys are placed first, followed by =>, followed by the key’s value. These “sets” are separated with a comma. Keys must either be a string or integer, while values can be of any type (including, yes, other arrays). You may have an infinite amount of key and value sets. Sets are usually written on new lines, to make the code easier to read, but it’s completely optional.
We can also choose to just specify a list of values:
[code='php']$array3 = array(1, 2, 3, 4, 5, 6, 7, 8, 9);[/code]
This will cause the keys to be automatically set to an incrementing number, starting from 0. So, the above array is equivalent to the following array:
[code='php']$array4 = array(0 => 1,
1 => 2,
2 => 3,
3 => 4,
4 => 5,
5 => 6,
6 => 7,
7 => 8,
8 => 9);[/code]
You can list an infinite amount of values in an array.
You may have noticed that attempting to echo or print an array results in an error. There is a special function for outputting entire arrays to a browser, print_r(). Which is used like this:
[code='php']$array5 = array('key0' => 'value0',
'key1' => 'value1');
print_r($array5);[/code]
And will output the following:
Array
(
[key0] => value0
[key1] => value1
)
It is also possible to retrieve the value of a specific key and also modify it. We can do that like so:
[code='php']echo $array5['key0'];
$array5['key1'] = 'banana';[/code]
Which would output value0 and change the value of the key key1 in $array5 to banana.
There are many aspects to arrays, and many functions available to manipulate them, that we cannot cover them all here. So, if you want to learn more about arrays, you can find more about them on their PHP page.
Foreach
Foreach is used to iterate over arrays. It allows you to loop through each array key and get its value.
[code='php']$array = array('1', '2', '3', '4', '5', '6');
foreach($array as $value) {
echo $value;
}[/code]
This will go through all the values of $array (starting from the beginning) and temporarily assign the current iteration’s value to $value, which you can then use within the curly brackets (in this example we just output the value to the browser). You’re free the call the $value variable whatever you want in the foreach statement.
If you’re dealing with more complex arrays, with keys and values, you can also choose to assign the key to a variable.
[code='php']$fruit_colours = array('banana' => 'yellow',
'apple' => 'red',
'pineapple' => 'yellow');
foreach($fruit_colours as $key => $value) {
echo $key . ': ' . $value;
}[/code]
This would assign the value of the key (the portion of the array before the =>) to $key and the corresponding value to $value.
If you need to ever modify the current key or value inside of a foreach loop, you can do so by assigning the variable by reference (this is PHP 5 and up only). So, let’s take our first example and change it to modify the current value instead.
[code='php']$array = array(1, 2, 3, 4, 5, 6);
foreach($array as &$value) {
$value = $value + 5;
}
unset($value);[/code]
As you can see, we set $value as a reference, then we’re able to modify that value within the foreach. After the foreach is complete, the $value reference will still exist, so we should destroy it using unset().
Closing statements
That concludes our article on arrays and foreach loops. Next week we’ll be looking at PHP’s most complex loop, for.