Download presentation
Presentation is loading. Please wait.
Published byAlicia Summers Modified over 9 years ago
1
Using Arrays in PHP (Part I) Lecture # 6
2
What is an array? It is a collection of multiple values assembled into one variable An array can consist of numbers and/or strings The array’s index can be numbers or strings You can use the superglobals $_POST or $_GET. Its indexes are the names of the form inputs. Therefore, $_POST[‘first_name’] refers to the value typed in a form input created by this code
3
Syntax Rules for naming arrays Begin with dollar sign Continue with a letter or underscore except superglobals which must start with an underscore after the $.
4
Creating an Array Use the array () function to create an array Example: $list = array (‘apples’, ‘bananas’, ‘oranges’); The first element in the previous example will be automatically indexed at 0 unless otherwise indicated. Example for specifying an index: $list = array (1 => ‘apples’, ‘bananas’, ‘oranges’); You can use the range function to create an array of items based on a range of values. Example: $ten = range (1, 10); // no need for single quotes for numbers $alpha = range (‘a’, ‘z’);
5
Code Example Script 7.1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> No Soup for You! Mmmmm...soups: <?php // Script 7.1 - soups1.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the array. $soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' ); // Try to print the array. print " $soups "; // Print the contents of the array. print_r ($soups); ?>
6
Adding items to an array If you do not specify an index for the new element you want to add then it will be automatically added after the last element in the array. If you use an index that already exists in the array the new element will override the old element in the array.
7
Code example Script 7.2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> No Soup for You! Mmmmm...soups: <?php // Script 7.2 - soups2.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the array. $soups = array ( 'Monday' => 'Clam Chowder', 'Tuesday' => 'White Chicken Chili', 'Wednesday' => 'Vegetarian' );
8
continued // Count and print the current number of elements. $number1 = count ($soups); print " The soups array originally had $number1 elements. "; // Add three items to the array. $soups['Thursday'] = 'Chicken Noodle'; $soups['Friday'] = 'Tomato'; $soups['Saturday'] = 'Cream of Broccoli'; // Count and print the number of elements again. $number2 = count ($soups); print " After adding 3 more soups, the array now has $number2 elements. "; print_r($soups); ?>
9
Creating Multidimensional Arrays Script 7.4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> My Books and Chapters <?php // Script 7.4 - books.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the first array. $phpvqs = array (1 => 'Getting Started', 'Variables', 'HTML Forms and PHP', 'Using Numbers'); // Create the second array. $phpadv = array (1 => 'Advanced PHP Programming', 'Object-Oriented Programming', 'Databases', 'Security'); // Create the third array. $phpmysql = array (1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to SQL and MySQL'); // Create the multidimensional array. $books = array ( 'PHP VQS' => $phpvqs, 'PHP Advanced VQP' => $phpadv, 'PHP and MySQL VQP' => $phpmysql );
10
continued // Print out some values. print " The third chapter of my first book is {$books['PHP VQS'][3]}. "; print " The first chapter of my second book is {$books['PHP Advanced VQP'][1]}. "; print " The fourth chapter of my third book is {$books['PHP and MySQL VQP'][4]}. "; // See what happens with foreach. foreach ($books as $key => $value) { print " $key: $value \n"; }
11
Nested foreach Script 7.4 modified <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> My Books and Chapters <?php // Script 7.4 - books.php // Address error handing. ini_set ('display_errors', 1); error_reporting (E_ALL & ~E_NOTICE); // Create the first array. $phpvqs = array (1 => 'Getting Started', 'Variables', 'HTML Forms and PHP', 'Using Numbers'); // Create the second array. $phpadv = array (1 => 'Advanced PHP Programming', 'Object-Oriented Programming', 'Databases', 'Security'); // Create the third array. $phpmysql = array (1 => 'Introduction to PHP', 'Programming with PHP', 'Creating Dynamic Web Sites', 'Introduction to SQL and MySQL');
12
continued // Create the multidimensional array. $books = array ( 'PHP VQS' => $phpvqs, 'PHP Advanced VQP' => $phpadv, 'PHP and MySQL VQP' => $phpmysql ); // See what happens with nested foreach. foreach ($books as $title => $chapters) { print " $title"; foreach ($chapters as $number => $chapter) { print " Chapter $number is $chapter"; } ?>
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.