http://www.chrispearson.org/pages/programming/php/array.asp
09h10
Wednesday, 8. October 2008

PHP: ARRAYS

ARRAYS - INTRODUCTION

Every high-level programming language supports arrays of one sort or another: a variable that can contain not just a simple character string or a numeric value but a matrix of many data elements. PHP provides rich support for arrays and techniques for processing them.

This article begins with a basic introduction to arrays and then looks at how they can be manipulated in PHP - It is broken down into pages which deal with individual topics and there is an index to this article at the bottom of this page. This page - the introduction - discusses arrays in quite general terms. There are side panels which give more formal definitions of terminology together with examples of different types of array.

At its most basic level an array can hold a simple list of values, maybe a shopping list like

  bread
  lemonade
  apples
  grapes
  coffee

 

If you think about writing code without arrays, every value would need to be stored using a simple variable. Each item in the shopping list would need to be assigned to a string variable using code like this

$item1 = "Bread";
$item2 = "Lemonade";
$item3 = "Apples";
$item4 = "Grapes";
$item5 = "Coffee";

which is perfectly good PHP code but printing the entire list would necessitate knowing - and coding - every variable name, for instance

print("<BR>$item1<BR>$item2<BR>$item3<BR>$item4<BR>$item5");

which, again, is perfectly good PHP code. But what happens when you add Eggs to the list? If Eggs goes into $item6 you'll need to change all the code that handles the shopping list, adding an assignment for $item6

$item6 = "Eggs";

and changing the print function to add a further HTML line break (<BR>) and the additional variable, $item6.

If, on the other hand, the shopping list was stored in an array, the list could hold one or more entries. The entries could be accessed by their index and we could use the following construct

$shoppinglist = array("Bread", "Lemonade",
   "Apples", "Grapes", "Coffee", "Eggs");

then code to implement this

SOME DEFINITIONS

A simple variable containing a single value is a scalar variable - It is a named location in which a string of character codes or a number can be stored

An array is a named location where a set of values may be stored

A value may be a string of character codes or a numeric quantity. Each value is known as an element of the array

Sometimes an array element is referred to as a member of the array

An array may have a numeric index - Each value in the set can be identified by a subscript and it is known as a numerically indexed array

An array with text keys or more-complex numeric keys is known as an associative array

(So we can say each element of an array has an index and a value)

for each element in array $shoppinglist
   print one value from $shoppinglist
loop for next element in $shoppinglist

This will print every value from the list whether there is one, ten, a hundred or a thousand. We'll return to the real PHP code to do this after we've looked at creating and filling arrays.

In PHP arrays are named in exactly the same way as other variables: A dollar sign followed by either an underscore character, an alphabetical character or a numeric character followed by any combination of underscores, aplha characters or numerics.

Each element of a PHP array has an index - in PHP this is also known as a key and in some older programming texts (and some not-so-old!) the index is referred to as a subscript. The index is used to access a particular element in the array.

In many programming languages array elements are referenced by a numeric index but in PHP the index (or key) can be an index number, a more complex (not necessarily sequential) numeric value or a character string.

We'll be returning to accessing elements using their keys but, as part of this introduction, suffice it to say that the index value is placed inside square brackets following the array name, as in

$shoppinglist[2]
$items[44]
$score["Home team"]
$score["Away team"]

which gives huge flexibility.

How arrays are indexed

Simple, numerically indexed list

Index Value
0 Bread
1 Lemonade
2 Apples
3 Grapes
4 Coffee
5 Eggs

More complex, keyed example of an associative array

Key Value
default.htm Home page
map.htm Site map
weather.htm Weather map
shopping.htm On-line store front

 

 

  Next page: Creating a PHP array >

Index to this article

Page Link Title Content
1 array.asp An Introduction to PHP Arrays

Introduction to arrays

Why use arrays?

Definitions

Examples of array types

2 array02.asp Using PHP arrays: Creating arrays

Creating arrays

Numerically indexed arrays

Associative arrays

3 array03.asp Using PHP arrays: Accessing arrays

Getting at an array's contents

Printing from arrays

4 array04.asp Using PHP arrays: Processing arrays

Adding to an array

Deleting array elements

Filling arrays from forms

Using arrays to write HTML

5 array05.asp PHP Reference sources Where to find out more about PHP and PHP programming

 

Top of this page

xxx,xxx

copyright ©2000 - 2008 Chris Pearson