Professional Documents
Culture Documents
B PHPArrays PDF
B PHPArrays PDF
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.
$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.
Numeric Arrays
Associative Arrays
Numeric Arrays
There are two ways to create a numeric array. Lets look at each example to see how we do that.
<?php
$employee_names[0] = "Dana";
$employee_names[1] = "Matt";
$employee_names[2] = "Susan";
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.
There are two ways to create a associative array. Lets look at each example to see how we do that.
<?php
$employee_title["Dana"] = "Owner";
$employee_title["Matt"] = "Manager";
$employee_title["Susan"] = "Cashier";
<?php
$employee_names = array("Dana" => "Owner", "Matt" => "Manager", "Susan" =>
"Cashier");
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.
Again, there are two ways to create a multidimensional array. Lets look at each example to see how
we do that.
<?php
$employees["employee 1"]["name"] = "Dana";
$employees["employee 1"]["title"] = "Owner";
$employees["employee 1"]["salary"] = "$60,000";
The above code creates an multidimensional array name employees and outputs "Matt is the Manager
and he earns $40,000 a year.".
<?php
$employees = array
(
"employee 1" => array
(
"name" => "Dana",
"title" => "Owner",
"salary" => "$60,000",
),
The above code creates an multidimensional array name employees and outputs "Dana is the Owner
and they earn $60,000 a year."
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>";
?>
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.