- Back to Home »
- PHP Tutorial »
- PHP Arrays
Posted by : senan
Wednesday, February 12, 2014
Welcome! In this section we cover what arrays are, what they are used for and how to work with arrays in PHP. But first…
An array is a mean to store collection of values in a single variable. Imagine, if you own a shop and you want to store the names of your employees. Now, instead of creating a separate variable to store each employee's name, you can use an array to store the names of all your employees in a single variable. This is how you would do it.
Example - PHP Array
$employee_names[0] = "Dana"; $employee_names[1] = "Matt"; $employee_names[2] = "Susan";
Each value in the array above is stored as an element and each element is associated to an id or a key which you see in the square brackets.
There are two types of arrays we deal with in PHP.
- Numeric Arrays
- Associative Arrays
Numeric Arrays
In a numeric array we store each element with a numeric ID key.
Creating an Numeric Array
There are two ways to create a numeric array. Lets look at each example to see how we do that.
Example 1 - Creating a Numeric Array in PHP
<?php $employee_names[0] = "Dana"; $employee_names[1] = "Matt"; $employee_names[2] = "Susan"; echo "The first employee's name is ".$employee_names[0]; echo "<br>"; echo "The second employee's name is ".$employee_names[1]; echo "<br>"; echo "The third employee's name is ".$employee_names[2]; ?>
<br> is a HTML tag for inserting line break. So, echo "<br>" inserts a line break after each sentence in our example above.
The above code create a numeric array employee_names and outputs...
"The first employee's name is Dana". "The second employee's name is Matt". "The third employee's name is Susan".
Example 2 - Creating a Numeric Array in PHP
The example below is an another way of creating a numeric array. Let's have a look.
<?php $employee_names = array("Dana", "Matt", "Susan"); echo "The third employee's name is ".$employee_names[2]; ?>
The above code create a numeric array employee_names and outputs "The third employee's name is Susan".
Associative Arrays
When we want to store elements in array with some meaningful association other than numbers, we use associative array. For example, let say we want to store our employee's job titles in an array. Using numeric wouldn't be too useful. Instead, we would use an associative array to associate each employee's name to its job title. Let's have a look at examples below.
Creating an Associative Array
There are two ways to create a associative array. Lets look at each example to see how we do that.
Example 1 - Creating a PHP Associative Array
<?php $employee_title["Dana"] = "Owner"; $employee_title["Matt"] = "Manager"; $employee_title["Susan"] = "Cashier"; echo "Matt is the ".$employee_title["Matt"]; ?>
Example 2 - Creating a PHP Associative Array
<?php $employee_names = array("Dana" => "Owner", "Matt" => "Manager", "Susan" => "Cashier"); echo "Matt is the ".$employee_title["Matt"]; ?>
In both examples above, we create an associative array $employee_title and output "Matt is the Manger".
Multidimensional Arrays
Lets extend our idea of associating a single key to an element. What if we want to associate multiple keys to an element? Consider the Employees Table/Matrix below.
Employees Table/Matrix
Hmmm, that's nice but how the heck would I store employees table in an array? Simple. PHP allows us to do that using multidimensional arrays. Let's look at the examples below to see how we would do that.
Creating Multidimensional Array in PHP
Again, there are two ways to create a multidimensional array. Lets look at each example to see how we do that.
Example 1 - Creating Multidimensional Array in PHP
<?php $employees["employee 1"]["name"] = "Dana"; $employees["employee 1"]["title"] = "Owner"; $employees["employee 1"]["salary"] = "$60,000"; $employees["employee 2"]["name"] = "Matt"; $employees["employee 2"]["title"] = "Manager"; $employees["employee 2"]["salary"] = "$40,000"; $employees["employee 3"]["name"] = "Susan"; $employees["employee 3"]["title"] = "Cashier"; $employees["employee 3"]["salary"] = "$30,000"; echo $employees["employee 2"]["name"]. " is the ".$employees["employee 2"]["title"]. " and he earns ".$employees["employee 2"]["salary"]. " a year."; ?>
The above code creates an multidimensional array name employees and outputs "Matt is the Manager and he earns $40,000 a year.".
Example 2 - Creating Multidimensional Array in PHP
<?php $employees = array ( "employee 1" => array ( "name" => "Dana", "title" => "Owner", "salary" => "$60,000", ), "employee 2" => array ( "name" => "Matt", "title" => "Manager", "salary" => "$40,000", ), "employee 3" => array ( "name" => "Susan", "title" => "Cashier", "salary" => "$30,000", ) ); echo $employees["employee 1"]["name"]. " is the ".$employees["employee 1"]["title"]. " and they earn ".$employees["employee 1"]["salary"]. " a year."; ?>
The above code creates an multidimensional array name employees and outputs "Dana is the Owner and they earn $60,000 a year."
Printing PHP Arrays?
To print the results of an array you can use the PHP function print_r() to print an array. See example below.
<?php $employee_title["Dana"] = "Owner"; $employee_title["Matt"] = "Manager"; $employee_title["Susan"] = "Cashier"; echo "<pre>"; print_r($employee_title); echo "</pre>"; ?>
The array result will print out as follow...
Array ( [Dana] => Owner [Matt] => Manager [Susan] => Cashier )
Note: Always remember to use <pre></pre> tags while printing an array as shown above otherwise the result is not very reader friendly.
Summary
So, in this section we covered an important lesson on arrays. You will find yourself using arrays a lot when programming in PHP because its the most easy and useful way of storing values. The usefulness of arrays will become more apparent in the next lesson, PHP Loops.