130 likes | 291 Views
Using Arrays in PHP (Part I). Lecture # 6. 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
E N D
Using Arrays in PHP(Part I) Lecture # 6
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 <input type=“text” name=“first_name” />
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 $.
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’);
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"> • <html xmlns="http://www.w3.org/1999/xhtml"> • <head> • <title>No Soup for You!</title> • </head> • <body> • Mmmmm...soups:<br /> • <?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 "<p>$soups</p>"; • // Print the contents of the array. • print_r ($soups); • ?> • </body> • </html>
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.
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"> • <html xmlns="http://www.w3.org/1999/xhtml"> • <head> • <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> • <title>No Soup for You!</title> • </head> • <body> • Mmmmm...soups:<br /> • <?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' • );
continued • // Count and print the current number of elements. • $number1 = count ($soups); • print "<p>The soups array originally had $number1 elements.</p>"; • // 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 "<p>After adding 3 more soups, the array now has $number2 elements.</p>"; • print_r($soups); • ?> • </body> • </html>
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"> • <html xmlns="http://www.w3.org/1999/xhtml"> • <head> • <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> • <title>My Books and Chapters</title> • </head> • <body> • <?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 • );
continued • // Print out some values. • print "<p>The third chapter of my first book is <i>{$books['PHP VQS'][3]}</i>.</p>"; • print "<p>The first chapter of my second book is <i>{$books['PHP Advanced VQP'][1]}</i>.</p>"; • print "<p>The fourth chapter of my third book is <i>{$books['PHP and MySQL VQP'][4]}</i>.</p>"; • // See what happens with foreach. • foreach ($books as $key => $value) { • print "<p>$key: $value</p>\n"; • } • </body></html>
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"> • <html xmlns="http://www.w3.org/1999/xhtml"> • <head> • <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> • <title>My Books and Chapters</title> • </head> • <body> • <?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');
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 "<p>$title"; • foreach ($chapters as $number => $chapter) { • print " <br />Chapter $number is $chapter"; • } • } • ?> • </p> • </body> • </html>