Download presentation
Presentation is loading. Please wait.
1
C SCI 116
2
What does $animals contain? 2 $animals = "ostrich"; $animals = "anteater"; $animals = "orangutan"; $animals = "cheetah"; $animals = "hyena";
3
An array contains a set of data represented by a single variable name A “list” of values 3 $animals[] “ostrich” “anteater” “orangutan” “cheetah” “hyena” $animals[] = "ostrich"; $animals[] = "anteater"; $animals[] = "orangutan"; $animals[] = "cheetah"; $animals[] = "hyena";
4
An element refers to a piece of data stored in an array An index is an element’s numeric position within the array By default, indexes start with zero (0) An array element is referenced by enclosing its index in brackets after the array name: print $animals[0]; 4
5
PHP has a number of built-in arrays Example: $_SERVER Also called autoglobal arrays $_SERVER $_POST $_GET $_COOKIE $_SESSION 5
6
Use print_r to quickly display array contents Not useful for user output 6 <?php print_r($_SERVER); ?> <?php print " "; print_r($_SERVER); print " "; ?> Much prettier
7
$array_name = array(values); 7 $animals[] = "ostrich"; $animals[] = "anteater"; $animals[] = "orangutan"; $animals[] = "cheetah"; $animals[] = "hyena"; – OR – $animals = array("ostrich", "anteater", "orangutan", "cheetah", "hyena");
8
for ($i=0; $i<sizeof($animals); $i++) { print $animals[$i]. " "; //print "{$animals[$i]} "; } ostrich anteater orangutan cheetah hyena ostrich anteater orangutan cheetah hyena
9
Used to loop through the elements in an array Does not require a counter Syntax: foreach ($array_name as $variable_name) { statements; } 9
10
foreach ($animals as $animal) { print "$animal "; } 10
11
Create an array called $names that contains the names of 5 of your classmates Print the array using print_r Print the first name in the array Print the last name in the array Print the array using a for loop Print the array using a for-each loop
12
12PHP Programming with MySQL With associative arrays, you specify an element’s key by using the array operator (=>) Syntax : $array_name = array(key=>value,...);
13
13PHP Programming with MySQL $capitals = array("Texas" => "Austen", "Oregon" => "Salem", "Ohio" => "Columbus", "New York" => "Albany"); print "The capital of Texas is ". $capitals['Texas']; print "The capital of Texas is {$capitals['Texas']}"; The capital of Texas is Austen
14
14PHP Programming with MySQL $capitals = array("Texas" => "Austen", "Oregon" => "Salem", "Ohio" => "Columbus", "New York" => "Albany"); foreach ($capitals as $state => $capital) { print "The capital of $state is $capital. "; } The capital of Texas is Austen. The capital of Oregon is Salem. The capital of Ohio is Columbus. The capital of New York is Albany. The capital of Texas is Austen. The capital of Oregon is Salem. The capital of Ohio is Columbus. The capital of New York is Albany.
15
Use count() or sizeof() to find the total number of elements in an array 15 $capitals = array("Texas" => "Austen", "Oregon" => "Portland", "Ohio" => "Columbus", "New York" => "Albany"); echo "There are ". count($capitals). " capitals."; There are 4 capitals.
16
str_split() and explode() functions split a string into an array str_split() uses a length argument to represent the number of characters to assign to each array element $array = str_split(string[, length]); explode() splits a string at a specified separator $array = explode(separators, string); 16
17
$months = "JanFebMarApr"; $month_array = str_split($months, 3); foreach($month_array as $month) echo "$month "; *Note that each element must have the same length 17
18
$animals = "ostrich,anteater, orangutan,cheetah,hyena"; $animal_array = explode(",", $animals); foreach ($animal_array as $animal) echo "$animal "; 18 ostrich anteater orangutan cheetah hyena ostrich anteater orangutan cheetah hyena Use the explode() function to convert a string to an array $array = explode(separators, string); If the string does not contain the specified separators, the entire string is assigned to the first element of the array
19
19 Use the implode() function to convert an array to a string $variable = implode(separators, array); If the string does not contain the specified separators, the entire string is assigned to the first element of the array $presArray = array("George W. Bush", "William Clinton", "George H.W. Bush", "Ronald Reagan", "Jimmy Carter"); $presidents = implode(", ", $presArray); echo $presidents;
20
20PHP Programming with MySQL array_shift() 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 Pass the name of an array followed by comma-separated values for each element you want to add
21
21PHP Programming with MySQL $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);
22
22PHP Programming with MySQL array_pop() removes the last element from the end of an array array_push() 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
23
23PHP Programming with MySQL $HospitalDepts = array( “Anesthesia”, “Molecular Biology”, “Neurology”, “Pediatrics”); array_pop($HospitalDepts); array_push($HospitalDepts, “Psychiatry”, “Pulmonary Diseases”); Anesthesia Molecular Biology Neurology Psychiatry Pulmonary Diseases Anesthesia Molecular Biology Neurology Psychiatry Pulmonary Diseases
24
24PHP Programming with MySQL The unset() function removes array elements Pass the array name and the index number of the element you want to remove unset($HospitalDepts[1]); unset($HospitalDepts[1], $HospitalDepts[2]); The array_values() function renumbers an indexed array’s elements
25
Make the following modifications to your $names array, and print the array after each step: 1. add a name to the beginning of the array 2. add two names to the end of the array 3. remove the first array element 4. remove the third array element
26
26PHP Programming with MySQL The array_unique() function removes duplicate elements from an array Pass the array name
27
27PHP Programming with MySQL $topGolfers = array( "Tiger Woods", "Tiger Woods", "Vijay Singh”, "Vijay Singh", "Ernie Els", "Phil Mickelson","Retief Goosen", "Padraig Harrington", "David Toms", "Sergio Garcia", "Adam Scott", "Stewart Cink"); echo "The world's top golfers are: ”; $topGolfers = array_unique($topGolfers); foreach ($topGolfers as $golfer) { echo "$golfer "; } echo " ";
28
28PHP Programming with MySQL in_array() returns Boolean TRUE if a given value exists in an array array_search() 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."; }
29
29PHP Programming with MySQL array_key_exists determines whether a given index or key exists array_key_exists($search_key, $array) $GamePieces["Dancer"] = "Daryl"; $GamePieces["Fat Man"] = "Dennis"; $GamePieces["Assassin"] = "Jennifer"; if (array_key_exists("Fat Man", $GamePieces)) { echo "{$GamePieces["Fat Man"]} is 'Fat Man'."; }
30
30PHP Programming with MySQL $GamePieces["Dancer"] = "Daryl"; $GamePieces["Fat Man"] = "Dennis"; $GamePieces["Assassin"] = "Jennifer"; if (array_key_exists("Fat Man", $GamePieces)) echo " {$GamePieces["Fat Man"]} is already 'Fat Man'. ”; else { $GamePieces["Fat Man"] = "Don"; echo " {$GamePieces["Fat Man"]} is now 'Fat Man'. ”; }
31
31PHP Programming with MySQL The most commonly used array sorting functions are: sort() and rsort() for indexed arrays ksort() and krsort() for associative arrays
32
32PHP Programming with MySQL sort() rsort()
33
Display your $names array in alphabetical order Display your $names array in reverse alphabetical order
34
34PHP Programming with MySQL To merge two or more arrays use the array_merge() function Syntax: new_array = array_merge($array1, $array2, $array3,...);
35
PHP Programming with MySQL 35 $mammals = array("cat", "dog", "bear"); $reptiles = array("snake", "lizard"); $birds = array("emu", "parakeet", "canary"); $animals = array_merge($mammals, $reptiles, $birds); print_r($animals); $mammals = array("cat", "dog", "bear"); $reptiles = array("snake", "lizard"); $birds = array("emu", "parakeet", "canary"); $animals = array_merge($mammals, $reptiles, $birds); print_r($animals); Array ( [0] => cat [1] => dog [2] => bear [3] => snake [4] => lizard [5] => emu [6] => parakeet [7] => canary ) Array ( [0] => cat [1] => dog [2] => bear [3] => snake [4] => lizard [5] => emu [6] => parakeet [7] => canary )
36
Create a second array called $family_names. Add at least three names to the array. Merge $names and $family_names into an array called $all_names. Remove any duplicates from $all_names. Print $all_names.
37
37PHP Programming with MySQL array_shift() removes the first element from the beginning of an array array_unshift() adds one or more elements to the beginning of an array array_pop() removes the last element from the end of an array array_push() adds one or more elements to the end of an array
38
38PHP Programming with MySQL unset() removes array elements array_unique() removes duplicate elements from an array in_array() returns a Boolean value of true if a given value exists in an array array_search() determines whether a given value exists in an array array_key_exists() determines whether a given index or key exists
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.