• Aucun résultat trouvé

Accessing arrays

Dans le document PHP 6 (Page 78-82)

You’ve already seen how to access indi-vidual array elements using its keys (e.g.,

$_POST[‘email’]). This works when you know exactly what the keys are or if you want to refer to only a single element.

To access every array element, use the

foreachloop:

foreach ($array as $value) { // Do something with $value.

}

Theforeachloop will iterate through every element in $array, assigning each element’s value to the $valuevariable. To access both the keys and values, use

foreach ($array as $key => $value) { echo “The value at $key is $value.”;

}

(You can use any valid variable name in place of $keyand$value, like just $kand$v, if you’d like.)

Using arrays, I’ll show how easy it is to make a set of form pull-down menus for selecting a date (Figure 2.19).

Programming with PHP

Introducing Arr a y s

Figure2.19These pull-down menus will be created using arrays and the foreach loop.

To create and access arrays:

1. Create a new PHP document in your text editor or IDE (Script 2.6).

<!DOCTYPE html PUBLIC “-//W3C//

DTD XHTML 1.0 Transitional//EN”

“http://www.w3.org/TR/xhtml1/DTD/

xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/

xhtml” xml:lang=”en” lang=”en”>

<head>

<?php # Script 2.6 - calendar.php

One thing to note here is that even though the page won’t contain a com-plete HTML form, the form tags are still required to create the pull-down menus.

Introducing Arr a y s

1 <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

“http://www.w3.org/TR/xhtml1/DTD/

xhtml1-transitional.dtd”>

2 <html xmlns=”http://www.w3.org/1999/xhtml”

xml:lang=”en” lang=”en”>

3 <head>

4 <meta http-equiv=”content-type”

content=”text/html; charset=

iso-8859-1” />

5 <title>Calendar</title>

6 </head>

7 <body>

8 <form action=”calendar.php” method=”post”>

9 <?php # Script 2.6 - calendar.php 10

11 // This script makes three pull-down menus

12 // for an HTML form: months, days, years.

13

14 // Make the months array:

15 $months = array (1 => ‘January’,

‘February’, ‘March’, ‘April’, ‘May’,

‘June’, ‘July’, ‘August’, ‘September’,

‘October’, ‘November’, ‘December’);

16

17 // Make the days and years arrays:

18 $days = range (1, 31);

19 $years = range (2008, 2018);

20

21 // Make the months pull-down menu:

22 echo ‘<select name=”month”>’;

23 foreach ($months as $key => $value) { 24 echo “<option value=\”$key\”>$value

</option>\n”;

25 }

26 echo ‘</select>’;

27

Script2.6Arrays are used to dynamically create three pull-down menus (see Figure 2.19).

(script continues on next page)

2. Create an array for the months.

$months = array (1 => ‘January’,

‘February’, ‘March’, ‘April’,

‘May’, ‘June’, ‘July’, ‘August’,

‘September’, ‘October’,

‘November’, ‘December’);

This first array will use numbers for the keys, from 1 to 12. Since the value of the first key is specified, the following values will be indexed incrementally (in other words, the 1 =>code creates an array indexed from 1 to 12, instead of from 0 to 11).

3. Create the arrays for the days of the month and the years.

$days = range (1, 31);

$years = range (2008, 2018);

Using the range()function, you can easily make an array of numbers.

4. Generate the month pull-down menu.

echo ‘<select name=”month”>’;

foreach ($months as $key => $value) { echo “<option value=\”$key\”>

$value</option>\n”;

}

echo ‘</select>’;

Programming with PHP

Introducing Arr a y s

continues on next page

28 // Make the days pull-down menu:

29 echo ‘<select name=”day”>’;

30 foreach ($days as $value) {

31 echo “<option value=\”$value\”>$value

</option>\n”;

32 }

33 echo ‘</select>’;

34

35 // Make the years pull-down menu:

36 echo ‘<select name=”year”>’;

37 foreach ($years as $value) {

38 echo “<option value=\”$value\”>$value

</option>\n”;

39 }

40 echo ‘</select>’;

41 42 ?>

43 </form>

44 </body>

45 </html>

Script2.6continued

Theforeachloop can quickly generate all of the HTML code for the month pull-down menu. Each execution of the loop will create a line of code like

<option value=”1”>January</option>

(Figure 2.20).

5. Generate the day and year pull-down menus.

echo ‘<select name=”day”>’;

foreach ($days as $value) { echo “<option value=\”$value\”>

$value</option>\n”;

}

echo ‘</select>’;

echo ‘<select name=”year”>’;

foreach ($years as $value) { echo “<option value=\”$value\”>

$value</option>\n”;

}

echo ‘</select>’;

Unlike the month example, both the day and year pull-down menus will use the same thing for the option’s value and label (a number, Figure 2.20).

6. Close the PHP, the form tag, and the HTML page.

?>

</form>

</body>

</html>

7. Save the file as calendar.php, place it in your Web directory, and test it in your Web browser.

Introducing Arr a y s

Tips

To determine the number of elements in an array, use the count()function.

$num = count($array);

Therange()function can also create an array of sequential letters:

$alphabet = range (‘a’, ‘z’);

An array’s key can be multiple-worded strings, such as first nameorphone number.

Theis_array()function confirms that a variable is of the array type.

If you see an Invalid argument supplied for foreach()error message, that means you are trying to use a foreachloop on a variable that is not an array.

Figure2.20Most of the HTML source was generated by just a few lines of PHP.

Dans le document PHP 6 (Page 78-82)