PHP: Indexed Arrays I
Derek Bridge
Department of Computer Science,
University College Cork
PHP: Indexed Arrays I
Aims:
- to appreciate some of the advantages of arrays
- to learn about PHP's indexed arrays (how to create, insert,
modify, and visit each element)
Life with arrays
- An array is a named collection of storage locations
- Think of it as several variables, but with just one name
- The array's individual storage locations can be easily accessed
- But equally, when necessary, the array can be moved around as a single unit
Indexed arrays
- An indexed array (or just array) is a collection of
of storage locations, indexed by position (starting from 0)
- To create an indexed array in PHP, e.g.:
$rainfalls = array(3, 2, 2, 1, 3);
- Another way to create an indexed array, e.g.
$rainfalls[0] = 3;
$rainfalls[1] = 2;
$rainfalls[2] = 2;
$rainfalls[3] = 1;
$rainfalls[4] = 3;
Indexed arrays
- You can also create an empty array, e.g.:
$toppings = array();
- To insert more values into the end of an existing indexed array, e.g.:
$toppings[] = 'mushrooms';
$toppings[] = 'anchovies';
$toppings[] = 'chocolate';
$toppings[] = 'baked beans';
Accessing the elements in an indexed array
- The values stored in an array are called the array elements
- In an indexed array, each element has a numeric index
- To overwrite a value in an existing array, e.g.
$toppings[1] = 'fried egg';
- To access a value in an array, e.g.
$my_favourite = $toppings[0];
echo "My favourite is {$my_favourite}. ";
echo "My second favourite is {$toppings[3]}.";
Non-consecutive indexes
- In PHP, indexed arrays may even have non-consecutive indexes
- E.g. in a class test, two people scored 4 out of 10; three people scored 8 out of 10:
$frequencies[4] = 2;
$frequencies[8] = 3;
- What happens if you access one of the 'missing' elements?
- Answer: The element is
NULL
, just like any uninitialized variable
- Class exercise: So what will happen here?
echo "The number of students who scored 5 out of 10 is: {$frequencies[5]}";
Doing things to each value in an indexed array
- The foreach statement:
foreach ($a as $v)
{
zero, one or more statements
}
- This is a loop
- It visits each element in array
$a
in turn
- For each element, it executes the statements in the curly braces
- Within these statements, it refers to the element as
$v
Class exercise: what is the output of the following?
echo "<ul>";
foreach ($toppings as $topping)
{
echo "<li>{$topping}</li>";
}
echo "</ul>";
Class exercise
- Rewrite the rainfall program, making it as concise as possible
- Is it better this way?
Observations
- Array size
- Element type
- In many programming languages, the elements must all be of the same
type, e.g. all integers, all strings
- In PHP, the elements can be a mixture of types...but we'll make
only limited use of this flexibility
- Indexes
- In most programming languages, the elements are indexed by consecutive
integers, starting from 0
- In PHP, there is a lot more flexibility
- However, arrays in other languages are much more efficient than in PHP