PHP examples: arrays and loops

Accessing elements in an array; 'for' loop


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP arrays
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 2/21/06
-->
<?php
$colors = array("red", "orange", "yellow");
$fruit = array("apple", "orange", "banana");
$td_style = "width: 75px; height: 30px; text-align: center";
?>
<html>
<head>
<title>
Using arrays to generate a table
</title>
</head>
<body>
<h3>What would you like for breakfast?</h3>
<table>
<tr>
<?php
print "<td style=\"background: $colors[0]; $td_style\">\n";
print "<div style=\"font-weight: bold\">$fruit[0]</div>\n";
print "</td>\n";

print "<td style=\"background: $colors[1]; $td_style\">\n";
print "<div style=\"font-weight: bold\">$fruit[1]</div>\n";
print "</td>\n";


print "<td style=\"background: $colors[2]; $td_style\">\n";
print "<div style=\"font-weight: bold\">$fruit[2]</div>\n";
print "</td>\n";
?>
</tr>
</table>
<table>
<tr>
<?php
  // it's more convenient to put together the table using a loop
for ($i = 0; $i < 3; $i++) {
  print "<td style=\"background: $colors[$i]; $td_style\">\n";
  print "<div style=\"font-weight: bold\">$fruit[$i]</div>\n";
  print "</td>\n";
 }
?>
</tr>
</table>
</body>
</html>

http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/arrays/arrays.php

Foreach loop


<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP arrays and foreach loops
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 2/23/06
-->
<?php
$todo = array("Do the laundry", "Study for the math test",
              "Finish writing the paper", "Call mom");
?>
<html>
<head>
<title>
Using a for-each loop
</title>
</head>
<body>
<h2>Your to-do list for the weekend</h2>
<ol>
<?php
foreach ($todo as $item) {
  print "<li>$item</li>\n";
}
?>
</ol>
</body>
</html>

http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/arrays/foreach.php

Associative arrays - arrays indexed with strings or other data

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
Associative arrays
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 2/23/06
-->
<?php
$phonebook = array("Sally" => "123-4567", "Joe" => "987-6543", 
"Mike" => "765-4321");
?>

<html>
<head>
<title>
Phone book example
</title>
</head>
<body>
<?php
// note that you have to use string concatenation here (see dots 
// surrounding $phonebook["Sally"]?), otherwise you run into a closing
// quotations problem
print "Sally's phone number is ".$phonebook["Sally"]."<br/>\n";
?>

<h3>All phone numbers in my phone book:</h3>
<?php
foreach ($phonebook as $name => $phone) {
	print "$name's phone number is $phone<br/>\n";	
}
?>
<h3>Added Jenny, removed Mike:</h3>
<?php
unset($phonebook["Mike"]); // removing an element of an array
$phonebook["Jenny"] = "192-8374"; // adding element to an array
foreach ($phonebook as $name => $phone) {
	print "$name's phone number is $phone<br/>\n";	
}
?>

<h3>This is how the array is stored in PHP</h3>
<?php
// print_r is a function that prints the array:
print_r($phonebook);
print "<br/>\n"; // print_r doesn't print a line break after the array data
// count is a function that counts the number of elements in the array
// notice the string concatenation below - we can't call a function 
// within a string
print "The array has ".count($phonebook)." elements.<br/>\n";
?>
<h3>Finding things in an array</h3>
<?php
if (in_array("123-4567", $phonebook)) {
	print "123-4567 is in the array<br/>\n";	
}
if (in_array(123, $phonebook)) {
	print "123 is in the array if you don't use strict comparison<br/>\n";	
}
if (in_array(123, $phonebook, true)) {
	print "123 is in the array with strict comparison<br/>\n";	
} else {
	print "123 is NOT in the array with strict comparison<br/>\n";	
}
if (array_key_exists("Joe",$phonebook)) {
	print "'Joe' is one of the keys of the array<br/>\n";	
}
print "All keys of the array are: ";
print_r(array_keys($phonebook));
print "<br/>\n";
?>

</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/arrays/assoc_arrays.php

Exercise on arrays

Create a 4-row table that has the lists seasons and sports: the season in the left column, the corresponding sport in the right one. Which of the loops would you use?

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP arrays - exercise
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 2/23/06
-->

<?php
$sports = array ("winter" => "skiing",
		 "spring" => "jogging",
		 "summer" => "kayaking",
		 "fall" => "hiking");		 
?>
<html>
<head>
<title>
Using arrays to generate a table

</title>
</head>
<body>
<?php
print_r($sports); // just testing - remove this line
?>
</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/arrays/arr_exercise.php

Two-dimensional arrays

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--
PHP two-dimensional arrays
Author: Elena Machkasova elenam@morris.umn.edu
Last modified: 3/2/06
-->
<?php
$data = array(array(2.8, 6.7, 4.5, 3.7, 5.0),
	      array(6.3, 7.8, 5.6, 8.0, 6.7, 7.9),
	      array(3.4, 5.2, 3.9, 4.6, 3.5));
$temperature = array("2/28/06" => array(24, 32, 41, 27),
		"3/1/06" => array(25, 30, 37, 19),
		"3/2/06" => array(15, 22, 26, 18));
?>
<html>
<head>

<title>
Two-dimensional arrays
</title>
</head>
<body>
<?php
print "<h3>data:</h3>\n"; 
print_r($data);
print "<h3>temperature:</h3>\n";
print_r($temperature);

print "<h3>Accessing an element in a 2D array:</h3>\n"; 
print $data[2][3]."<br/>\n";
print $temperature["3/1/06"][1]."<br/>\n";

// we use nested for loops here, we could use foreach instead (see below)
print "<h3>Finding the average of each row of data</h3>\n";
for ($i = 0; $i < count($data); $i++) {
	$sum = 0;
	for ($j = 0; $j < count($data[$i]); $j++) {
		$sum = $sum + $data[$i][$j];
	}
	$average = $sum/count($data[$i]);
	print "The average for row $i is $average<br/>\n";
}

print "<h3>Finding the average of each row of temperature</h3>\n";
foreach ($temperature as $day => $temps) {
	$sum = 0;
	foreach ($temps as $temp) {
		$sum = $sum + $temp;
	}
	$average = $sum/count($temps);
	// need brackets around $average, otherwise it's read as $averageF
	print "The average temperature for $day is {$average}F<br/>\n";
}
?>

</body>
</html>
http://rynite.morris.umn.edu/~elenam/1101_fall06/php_examples/arrays/twod_array.php