Home

Introduction to Arrays

Fundamentals of Arrays

Introduction

An array is a list of items where each item occupies a specific position. An array item can be a number, a letter, a string, an object, anything.

Creating an Array

The primary formula to create an array is:

$variable-name = array(item1, item2, ..., item-n)

The items in the list are created in the parentheses of array. Each item ends with a comma but you can omit the comma after the last item. Each item can be an integer. Here is an example:

$numbers = array(1, 36, 8, 15, 4, 286,)

An array can also contain floating-point numbers, strings, or Boolean values. Here are examples:

$numbers = array(19.95, 3.14156, 2.718, 4.0);
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");
$numbers = array(true, true, false, true);

The items of an array can be of different types. Here are examples:

$timesheet = array(208416, "Hermine", "Ngaleu", 24.45, 42.50, 1);

If the list of items is long, the items can be written on as many lines as you want. To make your code easy to read, you can write each item on its own line. Here is an example:

$timeZones = array(
    "Pacific Standard Time",
    "Mountain Standard Time",
    "Central Standard Time",
    "Eastern Standard Time",
    "Atlantic Standard Time",
    "Alaskan Standard Time",
    "Hawaii-Aleutian Standard Time",
    "Samoa standard time",
    "Chamorro Standard Time",
);

Introductory Operations on Arrays

Introduction to Accessing an Array Item

As mentioned in the introduction, each item of the list has a specific position. The position is also called an index. The positions are integer-based. The first item is at position 0, or has a 0 index. The second item is at position 1. The index of the third item is 2, and so on. This means that the first item is at index 0 and the last item is at index: number of items - 1.

To access an item, use the following formula:

$variable-name{ | [ index ] | }

Type the name of the variable. This is followed by either curly brackets or square brackets, as in $variable-name{index} or $variable-name[]. In the brackets, enter the index of the desired item. Here are examples:

<?php
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday");

echo $days[0] . ", ";
echo $days[1] . ", ";
echo $days[2] . ", ";
echo $days[3] . ", ";
echo $days[4]
?>

This would produce:

Accessing an Array Item

Since each item is recognized by its position, you may know what value is located where, and what that item represents. Consider the following example:

<?php
$timesheet = array(208416, "Hermine", "Ngaleu", 24.45, 42.50, 1);

echo "<h3>Employee Record</h3>";
echo "<pre>";
echo "Employee #:        " . $timesheet[0] . "<br>";
echo "First Name:        " . $timesheet[1] . "<br>";
echo "Last Name:         " . $timesheet[2] . "<br>";
echo "Hourly Salary:     " . $timesheet[3] . "<br>";
echo "Weekly Time:       " . $timesheet[4] . "<br>";
echo "Employment Status: " . $timesheet[5];
echo "</pre>"
?>

This would produce:

Accessing an Array Item

Remember that you can also use curly brackets to access an item. Here are examples:

<?php
$censusBureauRegions = array(
    "New England", "Mid-Atlantic", "East North Central",
    "West North Central", "South Atlantic", "East South Central",
    "West South Central", "Mountain", "Pacific"
);

echo "<h3>Census Bureau Regions</h3>";
echo "<pre>---------------------------------</pre>";
echo $censusBureauRegions{0} . "<br>";
echo $censusBureauRegions{1} . "<br>";
echo $censusBureauRegions{2} . "<br>";
echo $censusBureauRegions{3} . "<br>";
echo $censusBureauRegions{4} . "<br>";
echo $censusBureauRegions[5] . "<br>";
echo $censusBureauRegions[6] . "<br>";
echo $censusBureauRegions[7] . "<br>";
echo $censusBureauRegions[8]
?>

This would produce:

Accessing an Array Item

When accessing an item, if you provide an index that doesn't exist, normally, you would receive an error. In most cases, nothing would happen.

Using the Value of an Item

A list item is primarily recognized for its value, which can be anything. Because it holds a value, an item can be converted from one type to another. We mentioned that each item has an index that is integer-based. Imagine the value of an item A is an integer. That was the case for the employment status seen above:

Accessing an Array Item

Imagine that integer corresponds to the index of one of the items of an array. Consider the following example:

<?php
$employmentStatus = array("Part Time", "Full Time", "Contractor", "Seasonal");

echo "<h4>Employments Status</h4>";
echo $employmentStatus[0] . "<br>";
echo $employmentStatus[1] . "<br>";
echo $employmentStatus[2] . "<br>";
echo $employmentStatus[3]
?>

This would produce:

The Value of an Item

The access to the primary item can be used as an index to access the value of the item of the other array. This can be done as follows:

<?php
$employmentStatus = array("Part Time", "Full Time", "Contractor", "Seasonal");

$timesheet = array(208416, "Hermine", "Ngaleu", 24.45, 42.50, 1);

echo "<h3>Employee Record</h3>";
echo "<pre>";
echo "Employee #:        " . $timesheet[0] . "<br>";
echo "First Name:        " . $timesheet[1] . "<br>";
echo "Last Name:         " . $timesheet[2] . "<br>";
echo "Hourly Salary:     " . $timesheet[3] . "<br>";
echo "Weekly Time:       " . $timesheet[4] . "<br>";
echo "Employment Status: " . $employmentStatus[$timesheet[5]];
echo "</pre>"
?>

This would produce:

The Value of an Item

Array Items and Expressions

Once you can access a list item, you can use it as you see fit. For example, you can involve it in an expression or an operation. Here are examples:

<?php
$employmentStatus = array("Part Time", "Full Time", "Contractor", "Seasonal");

$timesheet = array(208416, "Hermine", "Ngaleu", 24.45, 42.50, 1);

// String Concatenation
$employeeName = $timesheet[2] . ", " . $timesheet[1];
// Arithmetic Operation
$weeklySalary = $timesheet[3] * $timesheet[4];

echo "<h3>Employee Record</h3>";
echo "<pre>-----------------------------------<br>";
echo "Employee #:        " . $timesheet[0] . "<br>";
echo "Employee Name:     " . $employeeName . "<br>";
echo "Employment Status: " . $employmentStatus[$timesheet[5]] . "<br>";
echo "Hourly Salary:     " . $timesheet[3] . "<br>";
echo "Time Worked:       " . $timesheet[4] . "<br>";
echo "Weekly Salary:     " . $weeklySalary . "<br>";
echo "------------------------------------</pre>"
?>

This would produce:

Array Items and Expressions

An Array as an Item

An item of an array can be an array itself. To specify the items of the internal array, use the same formula as for an array. Here is an example:

<?php
$timesheet = array(
    208416, "Hermine", "Ngaleu", 24.45,
    array(8.00, 8.00, 8.00, 8.00, 8.00), 
    1);
?>

In the same way, you can make as many items as you want act as arrays. To access the value of an internal item, type the name of the variable followed by {} or [] that contain the index of the item from the main array, followed by {} or [] that contains the index of the secondary item. Here are examples:

<?php
$timesheet = array(
    208416, "Hermine", "Ngaleu", 24.45,
    array(8.00, 8.50, 9.00, 9.00, 8.00), 
    1);

    $timeWorked = $timesheet{4}{0} + $timesheet{4}{1} + $timesheet{4}{2} +
                  $timesheet{4}{3} + $timesheet{4}{4};
echo "<h3>Employee Record</h3>";
echo "<pre>--------------------------------<br>";
echo "Employee #:        " . $timesheet[0] . "<br>";
echo "First Name:        " . $timesheet[1] . "<br>";
echo "Last Name:         " . $timesheet[2] . "<br>";
echo "Hourly Salary:     " . $timesheet[3] . "<br>";
echo "Employment Status: " . $timesheet[5] . "<br>";
echo "--------------------------------<br>";
echo "Time Worked        " . "<br>";
echo "Monday:            " . $timesheet{4}{0} . "<br>";
echo "Tuesday:           " . $timesheet{4}{1} . "<br>";
echo "Wednesday:         " . $timesheet{4}{2} . "<br>";
echo "Thursday:          " . $timesheet{4}{3} . "<br>";
echo "Friday:            " . $timesheet{4}{4} . "<br>";
echo "--------------------------------<br>";
echo "Weekly Time:       " . $timeWorked  . "<br>";
echo "</pre>"
?>

This would produce:

The Value of an Item


Previous Copyright © 2015-2022, FunctionX Next