8
PHP Programming with MySQL Slide 7-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

10
PHP Programming with MySQL Slide 7-9 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

11
PHP Programming with MySQL Slide 7-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

17
PHP Programming with MySQL Slide 7-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]);

18
PHP Programming with MySQL Slide 7-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

23
PHP Programming with MySQL Slide 7-22 Iterating Through an Array The internal array pointer refers to the currently selected element in an array Table 7-1 Array pointer iteration functions

24
PHP Programming with MySQL Slide 7-23 Iterating Through an Array Figure 7-7 Output of an array without advancing the internal array pointer

25
PHP Programming with MySQL Slide 7-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 “ The hospital has a Neurology department. ”;

26
PHP Programming with MySQL Slide 7-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

28
PHP Programming with MySQL Slide 7-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);

34
PHP Programming with MySQL Slide 7-33 Sorting Arrays 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

37
PHP Programming with MySQL Slide 7-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,...);

39
PHP Programming with MySQL Slide 7-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

48
PHP Programming with MySQL Slide 7-47 Working with Additional Dimensions Table 7-5 The Alaska table of a three-dimensional array

49
PHP Programming with MySQL Slide 7-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

50
PHP Programming with MySQL Slide 7-49 Summary 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

51
PHP Programming with MySQL Slide 7-50 Summary 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