0% found this document useful (0 votes)
23 views

WEBAPPS - Practice Exercise 8 - PHP Repetition Statement and Array

The document discusses PHP arrays and loops. It provides sample code to demonstrate declaring different types of arrays, including indexed, associative, and multidimensional arrays. The code also shows how to access array elements using foreach loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

WEBAPPS - Practice Exercise 8 - PHP Repetition Statement and Array

The document discusses PHP arrays and loops. It provides sample code to demonstrate declaring different types of arrays, including indexed, associative, and multidimensional arrays. The code also shows how to access array elements using foreach loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

WEBAPPS – PHP REPETITION STATEMENT

AND ARRAY
PRACTICE EXERCISE #8
Week 7 Date
Section Score

Instruction: Type the following codes in any text editor that you going to use like Notepad++. Understand
and analyze the codes. Run the program and understand the output of the program.

SAMPLE PROGRAM USING ARRAY AND FOREACH LOOP


1: <?php
2: //FILENAME: test6.php
3: //This program demonstrate how to declare array and use foreach in accessing array elements
4:
5: ini_set('display_errors',0);
6: error_reporting( E_ALL & ~ E_NOTICE);
7: ?>
8: <p>This program demonstrate how to declare array and use foreach in accessing array
elements.</p>
9: <?php
10: echo "<h1>Using foreach loop in array</h1>";
11: //index array
12: $array1 = array("Ramon","Emily","Lance","Iya");
13: $array4 = array(3=>"Ramon",7=>"Emily","Lance","Iya");
14: $array5 = range(10,20);
15: $array6 = range(30,50,3);
16: $array7 = range("a","z");
17: $array8 = range("a","z",3);
18: $array9 = range("A","z");
19: $array10 = range("A","z",5);
20:
21: //associative array
22: $array2 = array("Daddy"=>"Ramon","Mommy"=>"Emily","Son"=>"Lance","Daughter"=>"Iya");
23: $array3 = array("Aug"=>"August","Feb"=>"February","Mar"=>"March","Jul"=>"July");
24:
25:
26: echo "<p><b>Index Array</b></p>";
27: echo '$array1 = array("Ramon","Emily","Lance","Iya");<br>';

Prepared by: Prof. EMILY F. SICAT Page 1


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
AND ARRAY
PRACTICE EXERCISE #8
28: echo "Family Members <br>";
29: foreach($array1 as $key => $value)
30: {
31: echo ($key+1)." ".$value."<br>";
32: }
33:
34: echo "<br>";
35: echo '$array4 = array(3=>"Ramon",7=>"Emily","Lance","Iya");<br>';
36: echo "Family Members <br>";
37: foreach($array4 as $key => $value)
38: {
39: echo "$key $value<br>";
40: }
41:
42: echo "<p><b>Associative Array</b></p>";
43: echo '$array2 = array("Daddy"=>"Ramon","Mommy"=>"Emily","Son"=>"Lance","Daughter"=>"Iya");’;
44: echo "<br>Family Members <br>";
45: foreach($array2 as $term => $member)
46: {
47: echo ($term).": ".$member."<br>";
48: }
49:
50: echo "<br>";
51: echo '$array3 = array("Aug"=>"August","Feb"=>"February","Mar"=>"March","Jul"=>"July");<br>';
52: echo "Month of the Year <br>";
53: foreach($array3 as $key => $value)
54: {
55: echo ($key).": ".$value."<br>";
56: }
57: echo "<p><b>Using range() in creating array:</b></p>";
58: echo '$array5 = range(10,20);<br>';
59: foreach($array5 as $key => $value)
60: {
61: echo ($key).": ".$value."<br>";
62: }
63:
64: echo "<p><b>Using range() with step increment in creating array, appending array element and
overriding array element value :</b></p>";
65:

Prepared by: Prof. EMILY F. SICAT Page 2


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
AND ARRAY
PRACTICE EXERCISE #8
66: echo '$array6 = range(30,50,3);<br>';
67: echo '<b>Original value of $array6</b><br>';
68: foreach($array6 as $key => $value)
69: {
70: echo ($key).": ".$value."<br>";
71: }
72:
73: echo "<br>";
74: $array6[]="UMak";//appended to array $array6
75: $array6[]="Makati";//appended to array $array6
76: $array6[3]=2024;//overwrite the value of $array6[3]
77: echo '$array6[]="UMak";//appended to array $array6<br>';
78: echo '$array6[]="Makati";//appended to array $array6<br>';
79: echo '$array6[3]=2024;//overwrite the value of $array6[3]<br>';
80:
81: echo '<b>The new value of $array6 after appending and overriding</b><br>';
82: foreach($array6 as $key => $value)
83: {
84: echo ($key).": ".$value."<br>";
85: }
86:
87: echo "<p><b>Using range() in creating array assigning String value:</b></p>";
88: echo '$array7 = range("a","z");//index array<br>';
89: foreach($array7 as $key => $value)
90: {
91: echo ($key).": ".$value."<br>";
92: }
93:
94: echo "<br>";
95: echo '$array8 = range("a","z",3);//index array<br>';
96: foreach($array8 as $key => $value)
97: {
98: echo ($key).": ".$value."<br>";
99: }
100:
101: echo "<br>";
102: echo '$array9 = range("A","z");//index array<br>';
103: foreach($array9 as $key => $value)
104: {

Prepared by: Prof. EMILY F. SICAT Page 3


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
AND ARRAY
PRACTICE EXERCISE #8
105: echo ($key).": ".$value."<br>";
106: }
107:
108: echo "<br>";
109: echo '$array10 = range("A","z",5);//index array<br>';
110: foreach($array10 as $key => $value)
111: {
112: echo ($key).": ".$value."<br>";
113: }
114: ?>

TEST6.PHP OUTPUT

Prepared by: Prof. EMILY F. SICAT Page 4


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
AND ARRAY
PRACTICE EXERCISE #8

Prepared by: Prof. EMILY F. SICAT Page 5


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
AND ARRAY
PRACTICE EXERCISE #8
SAMPLE PROGRAM : 2D ARRAY AND FOREACH LOOP
1: <?php
2: //FILENAME: test7.php
3: //This program demonstrates how to create a two-dimensional array and use foreach in
accessing its array elements
4: ?>
5: <!DOCTYPE html>
6: <html>
7: <head>
8: <title>My Books and Chapters</title>
9: </head>
10: <body>
11: <?php
12: //address error handing
13: ini_set('display_errors',0);
14: error_reporting(E_ALL & ~E_NOTICE);
15:
16: //create the first array
17: $phpvqs = array(1=>'Getting Started','Variables','HTML Forms and PHP','Using Numbers');
18:
19: //create the second array
20: $phpadv = array(1=>'Advanced PHP Programming','Object-Oriented
Programming','Databases','Security');
21:
22: //create the third array
23: $phpmysql = array(1=>'Introduction to PHP','Programming with PHP','Creating Dynamic Web
Sites','Introduction to SQL and MySQL');
24:
25: //create the multidimensional array
26: $books = array( 'PHP VQS' => $phpvqs,
27: 'PHP Advanced VQP' => $phpadv,
28: 'PHP and MySQL VQP' => $phpmysql);
29:
30: echo "//create the first array<br>";
31: echo '$phpvqs = array(1=>"Getting Started","Variables","HTML Forms and PHP","Using
Numbers");<br><br>';
32:
33: echo "//create the second array<br>";

Prepared by: Prof. EMILY F. SICAT Page 6


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
AND ARRAY
PRACTICE EXERCISE #8
34: echo '$phpadv = array(1=>"Advanced PHP Programming","Object-Oriented
Programming","Databases","Security");<br><br>';
35: echo "//create the third array<br>";
36: echo '$phpmysql = array(1=>"Introduction to PHP","Programming with PHP","Creating Dynamic
Web Sites","Introduction to SQL and MySQL");<br><br>';
37:
38: echo "//create the multidimensional array<br>";
39: echo '$books = array( "PHP VQS" => $phpvqs,<br>
40: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"PHP Advanced VQP" =>
$phpadv,<br>
41: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"PHP and MySQL VQP" =>
$phpmysql);<br>';
42:
43: //print out some values
44: echo "<p><b>Print out some array values</b></p>";
45: print "<p>The third chapter of my first book is <i>{$books['PHP VQS'][3]}.</i></p>";
46: print "<p>The first chapter of my second book is <i>{$books['PHP Advanced
VQP'][1]}.</i></p>";
47: print "<p>The fourth chapter of my first book is <i>{$books['PHP and MySQL
VQP'][4]}.</i></p>";
48:
49: //see what happens with foreach
50: echo '<p><b>See what happens when using the key value pair in foreach loop using $books
array</b></p>';
51: foreach($books as $key => $value) {
52: echo "<p>$key : $value </p>";
53: }
54:
55: //print the two-dimensional array $books
56: echo '<p><b>Printing the $books two dimensional array</b></p>';
57: foreach($books as $title => $chapters)
58: {
59: print "<p><b>$title</b>";
60: foreach($chapters as $number => $chapter)
61: {
62: print("<br />Chapter $number is $chapter.");
63: }
64: print '</p>';
65: }

Prepared by: Prof. EMILY F. SICAT Page 7


Faculty, College of Computing and Information Sciences
WEBAPPS – PHP REPETITION STATEMENT
AND ARRAY
PRACTICE EXERCISE #8
66:
67: echo '<p><b>Printing the value of array $books using var_dump()</p></b>';
68: print "<pre><p>".var_dump($books)."</p></pre>";
69: ?>
70: </body>
71: </html>

TEST7.PHP OUTPUT

Prepared by: Prof. EMILY F. SICAT Page 8


Faculty, College of Computing and Information Sciences

You might also like