Home

Introduction to Lists

Fundamentals of Lists

Introduction

A list is a collection of values or objects. A list fundamentally provides the abilities to add new items to a list and to get (or retrieve) an existing item from the list. The other fundamental operations performed on a list are the abilities to change (or edit or update) the value of an existing item or to remove (or delete) an item from the list.

One of the most important characteristics of a list is the ability to know, at any type, the number of items that a list contains. The PHP array provides the necessary functionalities to create and manage a list.

Creating a List from Array

PHP provides the ability to create a list from an array. When creating or starting an array, you can indicate that it primarily has no value or no item. The formula to create such an array is:

$variable-name => array()

Based on this, to start a list, declare a variable for an array but leave the parentheses of array empty. Here is an example:

<?php $chemicalEements = array() ?>

Fundamental Operations on a List

Adding an Item to a List

Besdes creating a list, probably the must fundamental operation performed on a list consists of adding an item to it. To do this, apply empty square brackets to its variable and assign the desired value using the assignment operator =. Here is an example:

<?php
$chemicalEements = array();

$chemicalEements[] = "Hydrogen"
?>

In the same way, you can keep adding items to the list. Here are examples:

<?php
$chemicalEements = array();

$chemicalEements[] = "Hydrogen";
$chemicalEements[] = "Helium";
$chemicalEements[] = "Boron";
$chemicalEements[] = "Carbon";
$chemicalEements[] = "Nitrogen";
$chemicalEements[] = "Oxygen";
?>

An alternative to create a list is to omit formally declaring a variable and just directly start adding values to it. Here are examples:

<?php
$students[] = "Aaron";
$students[] = "Jennifer";
$students[] = "Yerimah";
?>

Getting an Item From a List

When you add the first item to a list, it gets an index of 0. The second item receives an index of 1. Ass you keep adding items, the receive an integral increment for their index. Using the features of the array, you can locate an item using its 0-based index. This is exactly as we did for a basic array. Here are examples:

<?php
$chemicalEements = array();

$chemicalEements[] = "Hydrogen";
$chemicalEements[] = "Helium";
$chemicalEements[] = "Boron";
$chemicalEements[] = "Carbon";
$chemicalEements[] = "Nitrogen";
$chemicalEements[] = "Oxygen";
 
echo "<h4>Chemical Elements</h4>";
echo "<pre>-----------------<br>";
echo $chemicalEements[0] . "<br>";
echo $chemicalEements{1} . "<br>";
echo $chemicalEements[2] . "<br>";
echo $chemicalEements[3] . "<br>";
echo $chemicalEements[4] . "<br>";
echo $chemicalEements[5] . "<br>";
echo "=================</pre>";
?>

This would produce:

Getting an Item From a List

Changing the Value of an Item of an Array or List

You can change the value of an item any time. To do this, access an item by its index and assign the desired value. Here are examples:

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

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

$timesheet[2] = "Sopngwi";
$timesheet[4] = 38.00;

$employeeName = $timesheet[2] . ", " . $timesheet[1];
$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:

The Value of an Item

Based on this feature, you can first create an array with empty values. When you have the necessary values, you can then store them in the array variable.

Array/List-Based Web Controls

The Combo Box

A combo box allows the user to select an item from a list of strings. To get a combo box, create an element using a tag named select. Here is an example:

<?php
 
echo "<h4>Electricity</h4>";
echo "Select the type of energy you want to explore: ";
echo "<select></select>";
?>

This would produce:

Introduction to The Combo Box

A combo box holds a list of strings. Each string is created as a sub-element of the select object. Each item of the combo box list is created using the option tag. To create an item in the combo box, adding a string between the starting and the closing tags of option. Here are examples:

<?php
$sources = array();

$sources[] = "Wind Power";
$sources[] = "Solar Energy";
$sources[] = "Tidal Energy";
$sources[] = "Fossil Fuel";
$sources[] = "Hydroelectricity";
$sources[] = "Generators";
 
echo "<h4>Electricity</h4>";
echo "Select the type of energy you want to explore: ";
echo "<select>";
echo "<option> $sources[0] </option>";
echo "<option> $sources[1] </option>";
echo "<option> $sources[2] </option>";
echo "<option> $sources[3] </option>";
echo "<option> $sources[4] </option>";
echo "<option> $sources[5] </option>";
?>

This would produce:

Introduction to The Combo Box

To select a default item from the combo box, add the selected attribute of its <option> tag. Here is an example:

<?php
$sources = array();

$sources[] = "Wind Power";
$sources[] = "Solar Energy";
$sources[] = "Tidal Energy";
$sources[] = "Fossil Fuel";
$sources[] = "Hydroelectricity";
$sources[] = "Generators";
 
echo "<h4>Electricity</h4>";
echo "Select the type of energy you want to explore: ";
echo "<select>";
echo "<option> $sources[0] </option>";
echo "<option> $sources[1] </option>";
echo "<option> $sources[2] </option>";
echo "<option selected> $sources[3] </option>";
echo "<option> $sources[4] </option>";
echo "<option> $sources[5] </option>";
echo "</select>"
?>

Optionally, you can specify it as selected='true'.

The List Box

Like a combo box, a list box displays a list of items (strings) that the user can select from. Unlike a combo box, a list box displays a taller list and can even display all of its items all the time.

Like a combo box, a list box is created using the <select> tag. Like the combo box, each item of a list box is created using the <option> tag. To make the control a list box, the <select> tag is equipped with an attribute named size. This attribute accomplishes two purposes: it makes the control become a list box and it sets the vertical size of the list box. In other words, it decides on the number of items the list box can display at a time. Here is an example:

<?php
$sdlc[] = "Planning";
$sdlc[] = "Analysis";
$sdlc[] = "Design";
$sdlc[] = "Implementation";
$sdlc[] = "Maintenance";

echo "<h2>Systems Development Life Cycle</h2>";
echo "<p>The systems development life cycle (SDLC) is a technique or process a 
team can follow to conduct a project. The general steps can be resumed as 
follows:</p>";
echo "<select size='5'>";
echo "<option> $sdlc[0] </option>";
echo "<option> $sdlc[1] </option>";
echo "<option> $sdlc[2] </option>";
echo "<option> $sdlc[3] </option>";
echo "<option> $sdlc[4] </option>";
echo "</select>"
?>

This would produce:

The List Box

A list box can be configured to allow the user to select one item. To select an item from the list, set the selected attribute of its <option> tag. Here is an example:

<?php
$sdlc[] = "Planning";
$sdlc[] = "Analysis";
$sdlc[] = "Design";
$sdlc[] = "Implementation";
$sdlc[] = "Maintenance";

echo "<h2>Systems Development Life Cycle</h2>";
echo "<p>The systems development life cycle (SDLC) is a technique or process a 
team can follow to conduct a project. The general steps can be resumed as 
follows:</p>";
echo "<select size='5'>";
echo "<option> $sdlc[0] </option>";
echo "<option> $sdlc[1] </option>";
echo "<option> $sdlc[2] </option>";
echo "<option selected> $sdlc[3] </option>";
echo "<option> $sdlc[4] </option>";
echo "</select>"
?>

Unlike a combo box, a list box can be configured to allow the user to select more than one item from the list. To allow this, include the multiple attribute in the <select> tag. Here is an example:

<?php
$sdlc[] = "Planning";
$sdlc[] = "Analysis";
$sdlc[] = "Design";
$sdlc[] = "Implementation";
$sdlc[] = "Maintenance";

echo "<h2>Systems Development Life Cycle</h2>";
echo "<p>The systems development life cycle (SDLC) is a technique or process a 
team can follow to conduct a project. The general steps can be resumed as 
follows:</p>";
echo "<select size='5' multiple>";
echo "<option> $sdlc[0] </option>";
echo "<option> $sdlc[1] </option>";
echo "<option> $sdlc[2] </option>";
echo "<option> $sdlc[3] </option>";
echo "<option> $sdlc[4] </option>";
echo "</select>"
?>

This would produce:

The List Box

Just including the multiple attribute in the <select> tag, even if you omit the size attribute, causes the control to be a list box

Looping Through a List or an Array

Introduction

Remember that each item of a list has an index, using a loop, you can access each item by its index and get its value. You can then use that value as you see fit. For example, you can display it in a web page or add it to a web control. Here is an example:

<?php
$shapes = array("Square", "Rectangle", "Parallelogram",
                "Triangle", "Pentagon", "Octagon", "Circle");

echo "<select size='7'>";
for($i = 0; $i <= 6; $i++)
    echo "<option> $shapes[$i] </option>";
echo "</select>";
?>

This would produce:

Looping Through a List or an Array

Consider an array that contains one or more items that is(are) array(s):

<?php
$apartments = array(
    array(101, 2, 2, 1150, 650, "Available"),
    array(102, 1, 1,  950, 500, "Needs Repair"),
    array(103, 1, 1,  925, 500, "Available"),
);
?>

The internal array can be accessed by applying its indexed square/curly brackets to the indexed square/curly brackets of the main array as in $apartments[0][0]. You can use the variable of a for loop as the index of the main array and access each internal item by its own index. Here is an example:

<?php
$apartments = array(
    array(101, 2, 2, 1150, 650, "Available"),
    array(102, 1, 1,  950, 500, "Needs Repair"),
    array(103, 1, 1,  925, 500, "Available"),
    array(104, 3, 2, 1350, 850, "Available"),
    array(105, 2, 1, 1150, 550, "Available"),
    array(106, 3, 2, 1350, 850, "Available"),
    array(107, 3, 2, 1285, 850, "Not Ready"),
    array(108, 1, 1,  885, 500, "Available"),
    array(109, 2, 2, 1150, 650, "Available"),
    array(110, 1, 1,  895, 500, "Available")
);

echo "<h3>Apartment Rental Management</h3>";

echo "<table border='5'>
       <tr>
         <td>Apt #</td>
         <td>Bedrooms</td>
         <td>Bathrooms</td>
         <td>Monthly Rate</td>
         <td>Security Deposit</td>
         <td>Status</td>
       </tr>";

for($i = 0; $i <= 9; $i++)
{
    echo "<tr><td>" . $apartments[$i][0] . "</td><td>" . 
                      $apartments[$i][1] . "</td><td>" . 
                      $apartments[$i][2] . "</td><td>" .
                      $apartments[$i][3] . "</td><td>" .
                      $apartments[$i][4] . "</td><td>" .
                      $apartments[$i][5] . "</td></tr>";
}
echo "</table>";
?>

This would produce:

Looping Through a List or an Array

For Each Item of a List/Array

PHP (like many modern languages) provides the ability to directly access each member of a list. The keyword to is foreach. There are two ways it can be used. One formula to use is:

foreach(list-name as $variable-name)
    statement

The keyword used is foreach and it uses parentheses. In the parentheses, first specify the name of the list or array. It is followed by the as keyword. Then specify a name for a variable. That name represents each item of the list or array. Outside the parentheses, create any desired statement(s). The statement can use the $variable-name in place of each list item. Here is an example:

<?php
$classifications = array("True Positive",
                         "True Negative",
                         "False Positive",
                         "False Negative");

echo "<h3>Binary Classification</h3>";
echo "<select size='4'>";
foreach($classifications as $classification)
    echo "<option>$classification</option>";
echo "</select>";
?>

This would produce:

For Each Item of a List/Array

If you are not planning to use the array variable many times, you dono't have to first declare its variable. You can define it directly in the parentheses of foreach. Here is an example:

<?php
echo "<h3>-- Binary Classification --</h3>";

echo "<select size='4'>";
foreach(array("True Positive",
              "True Negative",
              "False Positive",
              "False Negative") as $classification)
    echo "<option>$classification</option>";
echo "</select>";
?>

If an array is a member of another array, the foreach operator can be used to access the items of the external array and you can simply apply the brakets to the $variable-name to which you will apply the index of the item to access. Here is an exmple:

<?php
$apartments = array(
    array(111, 2, 2, 1145, 650, "Available"),
    array(112, 2, 1, 1085, 600, "Occupied"),
    array(201, 2, 1, 1185, 650, "Occupied"),
    array(202, 1, 1,  895, 500, "Available"),
    array(203, 1, 1,  925, 500, "Occupied"),
    array(204, 3, 2, 1250, 850, "Occupied"),
    array(205, 2, 1, 1100, 600, "Needs Repair"),
    array(206, 3, 2, 1300, 850, "Occupied"),
    array(207, 3, 2, 1350, 850, "Available"),
    array(208, 1, 1,  920, 500, "Not Ready"),
    array(209, 2, 2, 1150, 650, "Occupied"),
    array(210, 1, 1,  895, 500, "Needs Repair"),
    array(211, 2, 2, 1175, 650, "Not Ready"),
    array(212, 2, 1, 1075, 600, "Available")
);

echo "<h3>Apartment Rental Management</h3>";

echo "<table border='5'>
       <tr>
         <td>Apt #</td>
         <td>Bedrooms</td>
         <td>Bathrooms</td>
         <td>Monthly Rate</td>
         <td>Security Deposit</td>
         <td>Status</td>
       </tr>";

foreach($apartments as $apartment)
{
    echo "<tr><td>" . $apartment[0] . "</td><td>" . 
                      $apartment[1] . "</td><td>" . 
                      $apartment[2] . "</td><td>" .
                      $apartment{3} . "</td><td>" .
                      $apartment{4} . "</td><td>" .
                      $apartment{5} . "</td></tr>";
}
echo "</table";
?>

This would produce:

For Each Item of a List/Array

Changing the Value For Each Item

The foreach operator allows you to change the value of each list item. In each case, each item must be accessed by reference. To do this, the $variable-name in the parentheses must be preceded with & but not in the statement section.

<?php
$apartments = array(
    array(111, 2, 2, 1145, 650, "Available"),
    array(112, 2, 1, 1085, 600, "Occupied"),
    array(201, 2, 1, 1185, 650, "Occupied"),
    array(202, 1, 1,  895, 500, "Available"),
    array(203, 1, 1,  925, 500, "Occupied"),
    array(204, 3, 2, 1250, 850, "Occupied"),
    array(205, 2, 1, 1100, 600, "Needs Repair"),
    array(206, 3, 2, 1300, 850, "Occupied"),
    array(207, 3, 2, 1350, 850, "Available"),
    array(208, 1, 1,  920, 500, "Not Ready"),
    array(209, 2, 2, 1150, 650, "Occupied"),
    array(210, 1, 1,  895, 500, "Needs Repair"),
    array(211, 2, 2, 1175, 650, "Not Ready"),
    array(212, 2, 1, 1075, 600, "Available")
);

echo "<h3>Apartment Rental Management</h3>";

echo "<table border='5'>
       <tr>
         <td>Apt #</td>
         <td>Bedrooms</td>
         <td>Bathrooms</td>
         <td>Monthly Rate</td>
         <td>Security Deposit</td>
         <td>Status</td>
       </tr>";

foreach($apartments as &$apartment)
    $apartment[5] = "Available"
?>

Previous Copyright © 2015-2022, FunctionX Next