PHP Arrays: Defining, Looping and Sorting Simple Arrays

Unlike scalar variables, which assign only a single value to a variable, an array variable can hold multiple values.

Arrays are useful for holding values from database queries or web form entries, where each field (also called a “key”) holds a specific value.

Let’s take a look at how we define some of the arrays we use in PHP.Numbered Arrays.

If the programmer does not specify a key for each value in the array, PHP automatically numbers the keys, starting from zero.

This code defines an array $arrMonths[], with each month of the year as an element in the array.

<?php
$arrMonths[] = ‘January’;
$arrMonths[] = ‘February’;
$arrMonths[] = ‘March’;
$arrMonths[] = ‘April’;
?>

The PHP interpreter automatically defines each key in the array with a number, starting from zero.

<?php
$arrMonths[0] = ‘January’;
$arrMonths[1] = ‘February’;
$arrMonths[2] = ‘March’;
$arrMonths[3] = ‘April’;
?>

Array Function

Another method of defining an array is to use the array function:

<?php
$arrMonths= array(‘January’, ‘February’, ‘March’, ‘April’);
?>

This function creates a numbered array in the same way as the enumerated elements in the example above.

Associative Arrays

In some instances, the programmer does not want each value associated to a number, but to a more descriptive key.  Each of these descriptive keys needs to be associated with a value, hence the term “associative” array.

Just as with a numerical array, code authors can create an associative array one element at a time:

<?php
$arrBooks[‘Comic Books’] = ‘Superman’;
$arrBooks[‘Science Fiction’] = ‘Dune’;
$arrBooks[‘Fantasy’] = ‘The Hobbit’;
$arrBooks[‘Horror’] = ‘Carrie’;
?>

The array function is also useful for creating associative arrays. The “=>” symbol ties the key phrase to the value.

<?php
$arrBooks = array(
‘Comic’ => ‘Superman’,
‘Science Fiction’ => ‘Dune’,
‘Fantasy’ => ‘The Hobbit’,
‘Horror’ => ‘Carrie’);
?>

Is It An Array?

If you’re not certain if a variable has the structure of an array, the is_array function can test the variable to see if it is an array.

<?php
$baseballTeams = array(‘Cardinals’, ‘Tigers’, ‘Astros’);
$footballTeams = ‘Cardinals, Lions, Texans’;

if (is_array($baseballTeams)) {
echo ("Baseball Array<br>");
}
else {
echo ("No Baseball Array<br>");
}

if (is_array($footballTeams)) {
echo ("Football Array<br>");
}
else {
echo ("No Football Array<br>");
}

?>

Since the $baseballTeams variable is an array (defined by the array function) and the $footballTeams variable is a list of words separated by commas, the results of the code above will read:

Baseball Array
No Football Array

Looping Through Arrays

foreach loop runs through each element in an array. The author can display, run calculations or perform other operations on each element in the array as it comes through the loop.

<?php
$arrBooks = array(
‘Comic’ => ‘Superman’,
‘Science Fiction’ => ‘Dune’,
‘Fantasy’ => ‘The Hobbit’,
‘Horror’ => ‘Carrie’);

foreach ($arrBooks as $key => $value) {
print  "$value is an example of a $key book.<br>\n";
}
?>

The results will look like this:
Superman is an example of a Comic book.
Dune is an example of a Science Fiction book.
The Hobbit is an example of a Fantasy book.
Carrie is an example of a Horror book.

Sorting Arrays

The sort function allows for the sorting of array either numerically (for numerical values) or alphabetically (for string values). The sort function goes through each value and reassigns it to a new key.

<?php
$baseballTeams = array(‘Cardinals’, ‘Tigers’, ‘Astros’);
sort($baseballTeams);

foreach ($baseballTeams as $key => $value) {
echo $value. "<br>\n";
}
?>

The results will read:
Astros
Cardinals
Tigers

In next part of this series we will look at other functions that allow for the addition, deletion and manipulation of arrays.

Gerald Hanks has been involved in web development applications since 1996. He has designed applications with JavaScript, ASP.NET and PHP, as well as building databases in MS SQL Server and MySQL. He lives in Houston, Texas. More articles by Gerald Hanks
Home CSS Deals DesignBombs HTML HTML5 JavaScript jQuery Miscellaneous Mobile MySQL News PHP Resources Security Snippet Tools Tutorial Web Development Web Services WordPress