Web Systems Technologies Chapter 6 PHP Arrays Chapter

Web Systems & Technologies Chapter 6 - PHP Arrays Chapter 7 – Date and Time Functions 1

Numerically Indexed Arrays § An array with a numeric index. § The indices start at zero by default. § The index can be assigned automatically. – Each time a value is assigned, the first empty location is used to store the value, and a pointernal to PHP is incremented to point to the next free location. § Example 6 -1. Adding items to an array <? php $paper[] = "Copier"; $paper[] = "Inkjet"; $paper[] = "Laser"; $paper[] = "Photo"; print_r($paper); ? > // Array ( [0] => Copier [1] => Inkjet [2] => Laser [3] => Photo ) 2

Numerically Indexed Arrays § Example 6 -2. Adding items to an array using explicit locations <? php $paper[0] = "Copier"; $paper[1] = "Inkjet"; $paper[2] = "Laser"; $paper[3] = "Photo"; print_r($paper); ? > § Example 6 -3. Adding items to an array and retrieving them <? php $paper[] = "Copier"; $paper[] = "Inkjet"; $paper[] = "Laser"; $paper[] = "Photo"; for ($j = 0 ; $j < 4 ; ++$j) echo "$j: $paper[$j] "; ? > 3

Associative Arrays § Reference the items in an array by name rather than by number. § The names are called indexes or keys, and the items assigned to them are called values. § Example 6 -4. Adding items to an associative array and retrieving them <? php $paper['copier'] = "Copier & Multipurpose"; $paper['inkjet'] = "Inkjet Printer"; $paper['laser'] = "Laser Printer"; $paper['photo'] = "Photographic Paper"; echo $paper['laser']; ? > 4

Assignment Using the array Keyword § Assign values at once. § Using the format key => value, similiar to = but assigning a value to an index not a variable. § Example 6 -5. Adding items to an array using the array keyword <? php $p 1 = array("Copier", "Inkjet", "Laser", "Photo"); echo "p 1 element: ". $p 1[2]. " "; $p 2 = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); echo "p 2 element: ". $p 2['inkjet']. " "; ? > 5

The foreach. . . as Loop § Step through first item to last item in an array, one at a time. § In each iteration the array element is place in the variable following as keyword. § Loop ends when all values have been used. – You don’t have to know how many items are there in an array. § Example 6 -6. Walking through a numeric array using foreach. . . as <? php $paper = array("Copier", "Inkjet", "Laser", "Photo"); $j = 0; foreach($paper as $item) { echo "$j: $item "; ++$j; } ? > 6

The foreach. . . as Loop § Example 6 -7. Walking through an associative array using foreach. . . as <? php $paper = array('copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"); foreach($paper as $item => $description) echo "$item: $description "; ? > 7

Multidimensional Arrays § Ability to include an entire array as a part of another one, and so on. . § Example 6 -10. Creating a multidimensional associative array <? php $products = array( 'paper' => array( 'copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"), 'pens' => array( 'ball' => "Ball Point", 'hilite' => "Highlighters", Output: paper: copier (Copier & Multipurpose) paper: inkjet (Inkjet Printer) paper: laser (Laser Printer) paper: photo (Photographic Paper) pens: ball (Ball Point) …. 'marker' => "Markers"), 'misc' => array( 'tape' => "Sticky Tape", 'glue' => "Adhesives", 'clips' => "Paperclips" ) ); echo "<pre>"; foreach($products as $section => $items) foreach($items as $key => $value) echo "$section: t$keyt($value) "; echo "</pre>"; ? > 8

Multidimensional Arrays § Example 6 -11. Creating a multidimensional numeric array <? php $chessboard = array('r', 'n', 'b', 'q', 'k', 'b', 'n', 'r’), array('p', 'p', 'p’), array(' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ‘), array('P', 'P', 'P’), array('R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R') ); echo "<pre>"; foreach($chessboard as $row) { foreach ($row as $piece) echo "$piece "; echo " "; } echo "</pre>"; ? > 9

Using Array Functions § is_array • Check whether a variable is an array. – echo (is_array($fred)) ? "Is an array" : "Is not an array"; § count • count all the elements in the top level of an array. – echo count($fred); • count all the elements in of an array including all subarray element. – echo count($fred, 1); // optional (0 or 1) 1 forces recursive counting § shuffle • Puts array elements in random order • Acts directly on array and returs TRUE/FALSE on success/error – shuffle($cards); 10

Using Array Functions § sort • sorts an array. – sort($fred); • acts directly on array and returs TRUE/FALSE on success/error. • supports flags to force numeric or string sorting. – sort($fred, SORT_NUMERIC); – sort($fred, SORT_STRING); § rsort • sorts an array in reverse order. – rsort($fred); • acts directly on array and returs TRUE/FALSE on success/error. • supports flags to force numeric or string sorting. – rsort($fred, SORT_NUMERIC); – rsort($fred, SORT_STRING); 11

Using Array Functions § explode • Converts a string containing several items separated by a single character (or string of characters) into an array of these items. • Split up a sentence into an array containing all its words: <? php $temp = explode(' ', "This is a sentence"); print_r($temp); ? > //Array ( [0] => This [1] => is [2] => a [3] => sentence ) 12

Using Array Functions § extract • converts array keys into variable names and array values into variable value. • Example: $count = array("one" => "1", "two" => "2", "three" => "3"); extract($count); echo $two; § compact • creates an array from variables and their values. • requires the variable names to be supplied in quotes without $ symbol. • Example: <? php $fname = "Rasmus"; $lname = "Lerdorf "; $age = "51"; $contact = compact('fname', 'lname', 'age'); print_r($contact); ? > 13

Using Array Functions § reset • returns the value of first element of the array. – echo reset($contact); § end • returns the value of the last element of the array. – echo end($contact); 14

Date and Time Functions § PHP uses standard Unix timestamps, which are simply the number of seconds since the start of January 1, 1970. § Use the time function to determine the current timestamp: echo time(); § Use the mktime function to create a timestamp for a given date. echo mktime(0, 0, 0, 1, 1, 2000); • The parameters to pass are, in order from left to right: – – – Number of the hour (0– 23) Number of the minute (0– 59) Number of seconds (0– 59) Number of the month (1– 12) Number of the day (1– 31) Year (1970– 2038, or 1901– 2038) 15

Date and Time Functions § Use the date function to display a date($format, $timestamp); § The parameter $format should be a string containing formatting specifiers as detailed in Table 7 -4, and $timestamp should be a Unix timestamp. § Example: The following command will output the current date and time in the format "Thursday July 6 th, 2017 - 1: 38 pm": echo date("l F j. S, Y - g: ia", time()); 16

Using checkdate § To check whether a user has submitted a valid date to your program, pass the month, day, and year to the checkdate function. § Returns a value of TRUE if the date is valid, or FALSE if it is not. § Example 7 -3. Checking for the validity of a date <? php $month = 9; // September (only has 30 days) $day = 31; // 31 st $year = 2022; // 2022 if (checkdate($month, $day, $year)) echo "Date is valid"; else echo "Date is invalid"; ? > 17
- Slides: 17