PHP Tutorial 7 | Working with Arrays
29 October 2007 - 9:01
Arrays are a way of storing multiple values in one variable. In essence it is a way of storing separate pieces of information (either strings or numbers) in one variable. I think of it like a shopping list. For example my shopping list contains apples, oranges and peaches. In this example I can set a variable for my shopping list ($shlst) and set build an array out of my fruit.
Arrays function by assigning a key to each value in the array. In other words, each element in the array has its own ID allowing it to be accessed appropriately.
There are three types of arrays:
- A Numeric array - An array with a numeric ID key
- An Associative array - An array where each ID key is associated with a value
- And, a Multidimensional array - An array containing one or more arrays
Numeric Arrays
Going back to our shoping list variable ($shlst), lets go ahead and build a Numeric array. Well start by putting apples in our array. We’ll start by creating an array out of our apples, oranges and pears:
$shlst[0] = “Apples”;
$shlst[1] = “Oranges”;
$shlst[2] = “Peaches”;
Notice that all we did was set a variable: $shlst
assign a numerical key: [0]
and then set our value: =”apples”;
If I wanted to put that in array into a php script and have it output code I would simply do the following:
<?php
$shlst[0] = "Apples";
$shlst[1] = "Oranges";
$shlst[2] = "Peaches";
echo "Shopping List: ".$shlst[2]." ".$shlst[0]." and ".$shlst[1]."'s";
?>
The code above will give me the following output:
Shopping List: Peaches Apples and Oranges’s
In this code we have set the numeric key manually, but it is also possible to have the key automatically assigned. To do that we would simply write the array like this:
$shlst = “Apples”, “Oranges”, “Peaches”;
Notice that in this example there is no key []. That is because it is being auto assigned and you are not setting it manually. Also notice that each piece of fruit is surrounded by its own quotes and separated by a comma.
Associative Arrays
Associative arrays allow you to associate values to a name instead of using a numeric key. It is very useful because it allows you to freely associate values that relate to each other. For example lets say we have red apples, orange oranges, and pink peaches (i know they are more orange but hey, I wanted a different color).
Let’s put these things into an array so that you can see what I am talking about.
$fruit = array("Apples"=>Red, "Oranges"=>Orange, "Peaches"=>Pink);
What we have done here is assign created an array of fruit with colors associated to each piece. If we wanted to write it in code we could do the following:
<?php
$fruit = array("apples"=>Red, "oranges"=>Orange, "peaches"=>Pink);
echo "<p>Q. What color are apples? A.".$fruit['apples']."</p>";
echo "<p>Q. What color are oranges? A.".$fruit['oranges']."</p>";
echo "<p>Q. What color are peaches? A.".$fruit['peaches']."</p>";
?>
Looking at what we have done notice that we assigned the color red to “apples” and the color orange to “oranges” and finally the color pink to “peaches”.
Mulidimensional Arrays
Multidimensional arrays are a very powerful feature of PHP. In essence, each variable or element in the main array can also be a variable or element. This allows you great flexibility. For example if you had an $apple_type array you could break that up into arrays based on an $apple_color array which then linked apple colors to their exact names.
Let’s look at how that would work. Note that in the code below I chose to create an array that automatically assigns ID keys:
<?php
$apple_type = array
(
"Green_Apples"=>
array ( "Granny Smith" , "Golden Delicious"),
"Red_Apples"=>
array ( "Washington Apple" , "Paula Red")
);
echo "A " . $apple_type['Green_Apples'][0] .
" is definitely not a red apple like a ".
$apple_type['Red_Apples'][1].
".";
?>
Notice that we first created an array for $apple_type which we filled with two arrays for $Green_Apples and $Red_Apples. On outputting the results with the echo command we use the $apple_type[’Green_Apples’][0] to call up the final value of Granny Smith and $apple_type[’Red_Apples’][1] to call up the value Paula Red. It is important to note that the automatic key values start with zero rather than one. So, when calling up values you must take into account zero [0] as a placeholder.
The code above yields the following result:
A Granny Smith is definitely not a red apple like a PaulaRed.
No Comments | Tags: PHP

Loading ...


