1 / 50

Chapter 7 Manipulating Arrays PHP Programming with MySQL INFSCI 1092

Name of Book. 2. Objectives. Manipulate array elementsDeclare and initialize associative arraysUse iteration functionsFind and extract elements and valuesSort, combine, and compare arraysWork with multidimensional arrays. Name of Book. 3. Manipulating Elements. $Topic = $_POST['topic'];$Name

fawzi
Download Presentation

Chapter 7 Manipulating Arrays PHP Programming with MySQL INFSCI 1092

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


    1. Chapter 7 Manipulating Arrays PHP Programming with MySQL INFSCI 1092

    2. Name of Book 2 Objectives Manipulate array elements Declare and initialize associative arrays Use iteration functions Find and extract elements and values Sort, combine, and compare arrays Work with multidimensional arrays

    3. Name of Book 3 Manipulating Elements $Topic = $_POST['topic']; $Name = $_POST['name']; $Message = $_POST['message']; $PostMessage = addslashes(“$Topic~$Name~$Message\n”); $MessageStore = fopen(“messages.txt”, “a”); fwrite($MessageStore, “$PostMessage”); fclose($MessageStore); echo “<p><strong>Topic</strong>: $Topic<br />”; echo “<strong>Name</strong>: $Name<br />”; echo “<strong>Message</strong>: $Message</p>”;

    4. Name of Book 4 Manipulating Elements (continued) if (!file_exists(“messages.txt”) || filesize(“messages.txt”) == 0) echo “<p>There are no messages posted.</p>”; else { $MessageArray = file(“messages.txt”); for ($i=0; $i<count($MessageArray); ++$i) { $CurMessage = explode(“~”, $MessageArray[$i]); echo “<tr>”; echo “<td><strong>” . ($i + 1) . “</strong>.</td>”; echo “<td><strong>Topic</strong>: “ . stripslashes($CurMessage[0]) . “<br />”; echo “<strong>Name</strong>: “ . stripslashes($CurMessage[1]) . “<br />”; echo “<strong>Message</strong>: “ . stripslashes($CurMessage[2]); echo “</td></tr>”; } }

    5. Name of Book 5 Manipulating Elements (continued)

    6. Name of Book 6 Manipulating Elements (continued)

    7. Name of Book 7 Adding and Removing Elements from the Beginning of an Array The array_shift() function removes the first element from the beginning of an array Pass the name of the array whose first element you want to remove The array_unshift() function adds one or more elements to the beginning of an array Pass the name of an array followed by comma-separated values for each element you want to add

    8. Name of Book 8 Adding and Removing Elements from the Beginning of an Array (continued) $TopGolfers = array( “Ernie Els”, “Phil Mickelson”, “Retief Goosen”, “Padraig Harrington”, “David Toms”, “Sergio Garcia”, “Adam Scott”, “Stewart Cink”); array_shift($TopGolfers); array_unshift($TopGolfers, “Tiger Woods”, “Vijay Singh”); print_r($TopGolfers);

    9. Name of Book 9 Adding and Removing Elements from the Beginning of an Array (continued)

    10. Name of Book 10 Adding and Removing Elements from the End of an Array The array_pop() function removes the last element from the end of an array Pass the name of the array whose last element you want to remove The array_push() function adds one or more elements to the end of an array Pass the name of an array followed by comma-separated values for each element you want to add

    11. Name of Book 11 Adding and Removing Elements from the End of an Array (continued) $HospitalDepts = array( “Anesthesia”, “Molecular Biology”, “Neurology”, “Pediatrics”); array_pop($HospitalDepts); array_push($HospitalDepts, “Psychiatry”, “Pulmonary Diseases”);

    12. Name of Book 12 Adding and Removing Elements Within an Array The array_splice() function adds or removes array elements The array_splice() function renumbers the indexes in the array The syntax for the array_splice() function is: array_splice(array_name, start, characters_to_delete, values_to_insert);

    13. Name of Book 13 array_splice() Function To add an element within an array, include a value of 0 as the third argument $HospitalDepts = array( “Anesthesia”, // first element (0) “Molecular Biology”, // second element (1) “Neurology”, // third element (2) “Pediatrics”); // fourth element (3) array_splice($HospitalDepts, 3, 0, “Ophthalmology”);

    14. Name of Book 14 array_splice() Function (continued) To add more than one element within an array, pass the array() construct as the fourth argument Separate the new element values by commas $HospitalDepts = array( “Anesthesia”, // first element (0) “Molecular Biology”, // second element (1) “Neurology”, // third element (2) “Pediatrics”); // fourth element (3) array_splice($HospitalDepts, 3, 0, array(“Ophthalmology”, “Otolaryngology”));

    15. Name of Book 15 array_splice() Function (continued) Delete array elements by omitting the fourth argument from the array_splice() function $HospitalDepts = array( “Anesthesia”, // first element (0) “Molecular Biology”, // second element (1) “Neurology”, // third element (2) “Pediatrics”); // fourth element (3) array_splice($HospitalDepts, 1, 2);

    16. Name of Book 16 unset() Function The unset() function removes array elements and other variables Pass to the unset() function the array name and index number of the element you want to remove To remove multiple elements, separate each index name and element number with commas unset($HospitalDepts[1], $HospitalDepts[2]);

    17. Name of Book 17 Removing Duplicate Elements The array_unique() function removes duplicate elements from an array Pass to the array_unique() function the name of the array from which you want to remove duplicate elements The array_values() and array_unique() functions do not operate directly on an array The array_unique() function does renumber the indexes after removing duplicate values in an array

    18. Name of Book 18 array_unique()Function $TopGolfers = array( “Tiger Woods”, “Tiger Woods”, “Vijay Singh”, “Vijay Singh”, “Ernie Els”, “Phil Mickelson”, “Retief Goosen”, “Retief Goosen”, “Padraig Harrington”, “David Toms”, “Sergio Garcia”, “Adam Scott”, “Stewart Cink”); echo “<p>The world's top golfers are:</p><p>”; $TopGolfers = array_unique($TopGolfers); $TopGolfers = array_values($TopGolfers); for ($i=0; $i<count($TopGolfers); ++$i) { echo “{$TopGolfers[$i]}<br />”; } echo “</p>”;

    19. Name of Book 19 array_unique()Function (continued)

    20. Name of Book 20 Declaring and Initializing Associative Arrays With associative arrays, you specify an element’s key by using the array operator (=>) The syntax for declaring and initializing an associative array is: $array_name = array(key=>value, ...);

    21. Name of Book 21 Declaring and Initializing Associative Arrays (continued) $Territories[100] = “Nunavut”; $Territories[] = “Northwest Territories”; $Territories[] = “Yukon Territory”; print_r($Territories); echo '<p>The $Territories array consists of ', count($Territories), “ elements.</p>”;

    22. Name of Book 22 Iterating Through an Array The internal array pointer refers to the currently selected element in an array

    23. Name of Book 23 Iterating Through an Array (continued)

    24. Name of Book 24 Determining if a Value Exists The in_array() function returns a Boolean value of true if a given value exists in an array The array_search() function determines whether a given value exists in an array and Returns the index or key of the first matching element if the value exists, or Returns false if the value does not exist if (in_array(“Neurology”, $HospitalDepts)) echo “<p>The hospital has a Neurology department.</p>”;

    25. Name of Book 25 Determining if a Key Exists The array_key_exists() function determines whether a given index or key exists You pass two arguments to the array_key_exists() function: The first argument represents the key to search for The second argument represents the name of the array in which to search

    26. Name of Book 26 Determining if a Key Exists (continued) $GamePieces[“Dancer”] = “Daryl”; $GamePieces[“Fat Man”] = “Dennis”; $GamePieces[“Assassin”] = “Jennifer”; if (array_key_exists(“Fat Man”, $GamePieces)) echo “<p>{$GamePieces[“Fat Man”]} is already 'Fat Man'.</p>”; else { $GamePieces[“Fat Man”] = “Don”; echo “<p>{$GamePieces[“Fat Man”]} is now 'Fat Man'.</p>”; }

    27. Name of Book 27 Returning a Portion of an Array The array_slice() function returns a portion of an array and assigns it to another array The syntax for the array_slice() function is: array_slice(array_name, start, characters_to_return);

    28. Name of Book 28 Returning a Portion of an Array (continued) $TopGolfers = array(“Tiger Woods”, “Vijay Singh”, “Ernie Els”, “Phil Mickelson”, “Retief Goosen”, “Padraig Harrington”, “David Toms”, “Sergio Garcia”, “Adam Scott”, “Stewart Cink”); $TopFiveGolfers = array_slice($TopGolfers, 0, 5); echo “<p>The top five golfers in the world are:</p><p>”; for ($i=0; $i<count($TopFiveGolfers); ++$i) { echo “{$TopFiveGolfers[$i]}<br />”; } echo “</p>”;

    29. Name of Book 29 Returning a Portion of an Array (continued)

    30. Name of Book 30 Sorting Arrays The most commonly used array sorting functions are: sort() and rsort() for indexed arrays ksort() and krsort() for associative arrays

    31. Name of Book 31 Sorting Arrays (continued)

    32. Name of Book 32 Sorting Arrays (continued)

    33. Name of Book 33 Sorting Arrays (continued) Table 7-2 Array sorting functions (continued) If the sort() and rsort() functions are used on an associative array, the keys are replaced with indexes

    34. Name of Book 34 Sorting Arrays (continued)

    35. Name of Book 35 Sorting Arrays (continued)

    36. Name of Book 36 Combining Arrays To append one array to another, use the addition (+) or the compound assignment operator (+=) To merge two or more arrays use the array_merge() function The syntax for the array_merge() function is: new_array = array_merge($array1, $array2, $array3, ...);

    37. Name of Book 37 Combining Arrays (continued) $Provinces = array(“Newfoundland and Labrador”, “Prince Edward Island”, “Nova Scotia”, “New Brunswick”, “Quebec”, “Ontario”, “Manitoba”, “Saskatchewan”, “Alberta”, “British Columbia”); $Territories = array(“Nunavut”, “Northwest Territories”, “Yukon Territory”); $Canada = $Provinces + $Territories; print_r($Canada);

    38. Name of Book 38 Comparing Arrays The array_diff() function returns an array of elements that exist in one array but not in any other arrays to which it is compared The syntax for the array_diff() function is: new_array = array_diff($array1, $array2, $array3, ...); The array_intersect() function returns an array of elements that exist in all of the arrays that are compared

    39. Name of Book 39 Comparing Arrays (continued) The syntax for the array_intersect() function is: new_array = array_intersect($array1, $array2, $array3, ...);

    40. Name of Book 40 Comparing Arrays (continued)

    41. Name of Book 41 Creating Two-Dimensional Indexed Arrays A multidimensional array consists of multiple indexes or keys A two-dimensional array has two sets of indexes or keys

    42. Name of Book 42 Creating Two-Dimensional Indexed Arrays (continued) $USDollars = array( 104.6100, // Yen 0.7476, // Euro 0.5198, // UK Pound 1.2013, // Canadian Dollar 1.1573 // Swiss Francs );

    43. Name of Book 43 Creating Two-Dimensional Indexed Arrays (continued) $USDollars = array(1, 104.61, 0.7476, 0.5198, 1.2013, 1.1573); $Yen = array(0.009559, 1, 0.007146, 0.004969, 0.011484, 0.011063); $Euro = array(1.3377, 139.9368, 1, 0.6953, 1.6070, 1.5481); $UKPound = array(1.9239, 201.2592, 1.4382, 1, 2.3112, 2.2265); $CanadianDollar = array(0.8324, 87.0807, 0.6223, 0.4327, 1, 0.9634); $SwissFranc = array(0.8641, 90.3914, 0.6459, 0.4491, 1.0380, 1);

    44. Name of Book 44 Creating Two-Dimensional Indexed Arrays (continued) $ExchangeRates = array($USDollars, $Yen, $Euro, $UKPound, $CanadianDollar, $SwissFranc); Table 7-4 Elements and indexes in the $ExchangeRates[ ] array

    45. Name of Book 45 Creating Two-Dimensional Associative Arrays

    46. Name of Book 46 Creating Multidimensional Arrays with a Single Statement $ExchangeRates = array( array(1, 104.61, 0.7476, 0.5198, 1.2013, 1.1573), // U.S. $ array(0.009559, 1, 0.007146, 0.004969, 0.011484, 0.011063), // Yen array(1.3377, 139.9368, 1, 0.6953, 1.6070, 1.5481), // Euro array(1.9239, 201.2592, 1.4382, 1, 2.3112, 2.2265), // U.K. Pound array(0.8324, 87.0807, 0.6223, 0.4327, 1, 0.9634), // Canadian $ array(0.8641, 90.3914, 0.6459, 0.4491, 1.0380, 1) // Swiss Franc );

    47. Name of Book 47 Working with Additional Dimensions Table 7-5 The Alaska table of a three-dimensional array

    48. Name of Book 48 Summary The array_shift() function removes the first element from the beginning of an array The array_unshift() function adds one or more elements to the beginning of an array The array_pop() function removes the last element from the end of an array The array_push() function adds one or more elements to the end of an array The array_splice() function adds or removes array elements

    49. Name of Book 49 Summary (continued) The unset() function removes array elements and other variables The array_values() function renumbers an indexed array’s elements The array_unique() function removes duplicate elements from an array The in_array() function returns a Boolean value of true if a given value exists in an array The array_search() function determines whether a given value exists in an array

    50. Name of Book 50 Summary (continued) The array_key_exists() function determines whether a given index or key exists The array_slice() function returns a portion of an array and assigns it to another array The array_diff() function returns an array of elements that exist in one array but not in any other arrays to which it is compared The array_intersect() function returns an array of elements that exist in all of the arrays that are compared

More Related