Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
PHP Programming With MySQL Second Edition.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
43.07 Mб
Скачать

Iterating Through an Array

5.

If the statement list[] = 'B'; immediately follows the

statement list[25] = 'A';, what is the array index for the

element whose value is ‘B’?

323

Iterating Through an Array

In Chapter 2, you learned how to use a foreach statement to iterate

through the elements in an array. As a refresher, the following exam-

ple declares and initializes an indexed array named $DaysOfWeek[]

and uses a foreach statement to iterate through it:

$DaysOfWeek = array("Sunday", "Monday",

"Tuesday", "Wednesday", "Thursday",

"Friday", "Saturday");

foreach ($DaysOfWeek as $Day) {

echo "<p>$Day</p>\n";

}

Even though a foreach statement allows you to loop through the

elements of an array, it does not change the position of the internal

array pointer, a special type of variable that refers to the currently

selected element in an array. The internal array pointer is a way of

keeping track of which element you are working with in an array. Use

the functions listed in Table 6-1 to iterate through an array with the

internal array pointer.

Function

each(array)

Description

Returns the key and value of the current array element

and moves the internal array pointer to the next

element

Moves the internal array pointer to the last element

Returns the key of the current array element

Moves the internal array pointer to the next element

Moves the internal array pointer to the previous element

Resets the internal array pointer to the first element

current(array) Returns the current array element

end(array)

key(array)

next(array)

prev(array)

reset(array)

Table 6-1

Array pointer iteration functions

As a simple example of how to use the iteration functions, the

next() function in the following code moves the internal array

pointer in the $DaysOfWeek[] array to the second array element

(“Monday”), whereas the end() function moves the internal array


CHAPTER 6

Manipulating Arrays

pointer to the final array element (“Saturday”). The echo statements

use the current() function to display the value of the element in the

$DaysOfWeek[] array where the internal array pointer is located.

next($DaysOfWeek);

echo "<p>" . current($DaysOfWeek) . "</p>\n";

end($DaysOfWeek);

echo "<p>" . current($DaysOfWeek) . "</p>\n";

324

You might be wondering why you need iteration functions at all. Why

not just use a foreach statement or other type of looping statement

to iterate through an array? For indexed arrays, a looping statement is

usually all you need to work with the elements in an array. However,

because the keys in an associative array might not be in a predict-

able sequence, you can’t always use a looping statement to determine

which element you are currently working with in associative arrays.

For example, consider the following code, in which a foreach state-

ment is used to display the values in the $ProvincialCapitals[]

array. To display an element key (which, in this case, contains the

name of each province), you can use the key() function. However,

the key() function only returns the key of the element at the loca-

tion of the internal array pointer. Because the internal array pointer

points to the first element by default, the following code displays the

first element value (“Newfoundland and Labrador”), as shown in

Figure 6-8:

$ProvincialCapitals = array(

"Newfoundland and Labrador" => "St. John's",

"Prince Edward Island" => "Charlottetown",

"Nova Scotia" => "Halifax",

"New Brunswick" => "Fredericton",

"Quebec" => "Quebec City",

"Ontario" => "Toronto",

"Manitoba" => "Winnipeg",

"Saskatchewan" => "Regina",

"Alberta"=>"Edmonton",

"British Columbia" => "Victoria");

foreach ($ProvincialCapitals as $Capital) {

echo "The capital of " .

key($ProvincialCapitals) .

" is $Capital<br />\n";

}

For the

Windows

version of

the PHP

scripting

engine, the key() func-

tion may not work properly

within a foreach loop.

Instead, you should use

the advanced foreach

syntax described in the

next section.


Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]