1 / 20

php Arrays (Part 1) MIS 4530 Dr. Garrett

php Arrays (Part 1) MIS 4530 Dr. Garrett. Manipulating Elements. $Topic = $_POST['topic']; $Name = $_POST['name']; $Message = $_POST['message']; $PostMessage = addslashes(“$Topic~$Name~$Message<br>”); $MessageStore = fopen(“messages.txt”, “a”); fwrite($MessageStore, “$PostMessage”);

lei
Download Presentation

php Arrays (Part 1) MIS 4530 Dr. Garrett

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. php Arrays (Part 1)MIS 4530Dr. Garrett

  2. 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>”;

  3. 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>”; } }

  4. Manipulating Elements (continued) Figure 7-1 Post New Message page of the Discussion Forum script

  5. Manipulating Elements (continued) Figure 7-2 Message Posted page of the Discussion Forum script

  6. 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

  7. 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);

  8. Adding and Removing Elements from the Beginning of an Array (continued) Figure 7-3 Output of an array modified with the array_shift() and array_unshift() functions

  9. 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

  10. 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”,“PulmonaryDiseases”);

  11. 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);

  12. 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”);

  13. 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”));

  14. 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);

  15. 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]);

  16. 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

  17. 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>”;

  18. array_unique()Function (continued) Figure 7-4 Output of an array after removing duplicate values with the array_unique() function

  19. 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, ...); Figure 7-5 Output of array with associative and indexed elements

  20. 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>”; Figure 7-6 Output of an array with a starting index of 100

More Related