This site is an assignment site for study group of Hello World forum.
Together we are studying the book Larry Ullman PHP For World Wide Web.

Chapter 7: Using Arrays

What Is an Array?

Arrays are essential for storing, managing, and operating on sets of variables. It can hold more information than a simple string.

Syntactical rules for arrays:

Greating an Array

$days = array (1 => 'Monday' , 2 => 'Tuesday' , 3 => 'Wednesday');

Adding Items to an Array

An array looks the same than a variable, except it can include a key, the key needs to set into a square brackets([]). It's optional to specify the key or not.

Merging Arrays: the function array_merge()

Deleting Arrays and Array Elements: the function unset()

Accessing Array Elements:the function foreach

Foreach function loop iterates through every element of the the unkeyed array, assigning each index and each value.

Sorting Arrays

Arrays Sorting Functions
Function Sorts by Maintains Key Values
sort() values no
rsort() values inverse no
asort() values yes
arsort() values inverse yes
ksort() keys yes
krsort() keys inverse yes

extract() function can be used to access array values.

list() function for retrievingvalues from a database.

implode() function turns an array into a string.

explode() function turns a string into an array.

Creating an array from a form

Name the Event into this next textfield and choose a day or days.

Event Name:

Week Days: S M T W T F S


The Source Code

<?php 
//Variables for the phpwww site
$websitename "PHP for World Wide Web";// tittle of website

?>
<!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=UTF-8" />
<meta name="author" content="Kristiina Hillerström" />
<meta name="Description" content="An assignment site for studygroup Hello World PHP 101." />
<title><?php echo "$websitename"?></title>
<link href="/phpwww/css/phpwww.css" rel="stylesheet" type="text/css" />
<link href="/phpwww/css/calculator.css" rel="stylesheet" type="text/css" />
<!--[if lte IE 7]>  
<style type="text/css" media="all">  
@import "/phpww/css/fieldset-styling-ie.css";  
</style> 
<![endif]-->
<!--[if IE 6]>
<style type="text/css">

body {
    font-size: 80%;
}

</style>
<![endif]-->
</head>
<body >
<div id="container">
  <!-- start #container -->
  <div id="header"> <img src="images/pw_header.jpg" width="780" height="61" alt="header" />
    <h1>PHP for World Wide Web</h1>
    <!-- end #header -->
  </div>
  <!-- start navi  -->
  <div id="navcontainer">
    <ul id="navlist">
      <li><a href="/phpwww/index.php">Index</a></li>
      <?php //for-loop to print menu usind div and list
       
  
for ($i 1$i <= 13$i++) {
      echo 
"<li><a href='ch$i.php'>Ch$i</a></li>";
  }
   
?>
      <li><a href="index.en.html">Home</a></li>
    </ul>
  </div>
  <!-- start #mainContent -->
  <div id="mainContent">
    <p class="tittle">This site is an assignment site for study group of 
    <a href="http://lzydaz.com/phpBB3/index.php?sid=71b334691616fb09a7fae8b7a2c2f477"> Hello World</a> forum.<br />
      Together we are studying the book <a href="http://www.dmcinsights.com/phpvqs2/">
       Larry Ullman PHP For World Wide Web.</a></p>
    <h4 class="padding_left">Chapter 7: Using Arrays</h4>
    <ul>
      <li>What Is an Array?<br />
        Creating an Array<br />
        Adding Items to an Array<br />
        Accessing Array Elements<br />
        Creating Multidimensional Arrays<br />
        Sorting Arrays<br />
        Transforming Between Strings and Arrays<br />
      Creating an Array from a Form</li>
    </ul>
    <h3>What Is an Array?</h3>
    <p>Arrays are essential for storing, managing, and operating on sets of variables.
     It can hold more information than a simple string.    </p>
    <h3 class="h3info">Syntactical rules for arrays:</h3>
<ul>
      <li class="h3info">Begin with a dollar sign.</li>
      <li class="h3info">Continue with a letter or underscore.</li>
      <li class="h3info">Finnish with any combination of letters, numbers or the underscore</li>
</ul>
<h3>Greating an Array</h3>
<p>$days = array (1 =&gt; 'Monday' , 2 =&gt; 'Tuesday' , 3 =&gt; 'Wednesday');</p>
<h3>Adding Items to an Array</h3>
<p>An array looks the same than a variable, except it can include a key, 
the key needs to set into a square brackets([]). It's optional to specify the key or not.</p>
<ul>
  <li>$days[] ='Monday';<br />
    $days[] ='Tuesday';</li>
  <li>
    <p>Any existing value already indexed do get overwritten in that location  when you 
    specify the key.</p>
  </li>
  <li>$days[1] ='Monday';</li>
  <li>$days[2] ='Tuesday';</li>
</ul>


<p>Merging Arrays: the function <a href="http://fi2.php.net/manual/en/function.array-merge.php">array_merge()</a></p>
<ul>
  <li>$new_array = array_merge ($array1,$array2);</li>
</ul>
<p>Deleting Arrays and Array Elements: the function <a href="http://fi2.php.net/manual/en/function.unset.php">unset()</a></p>
<ul>
  <li>unset ($array[4]]; or unset ($array['name']); or reseting an array $array = array()</li>
</ul>
<p>Accessing Array Elements:the function<a href="http://fi2.php.net/manual/en/control-structures.foreach.php"> foreach</a></p>
<p>Foreach function loop iterates through every element of the the unkeyed array,
 assigning each index and each value.</p>
<ul>
  <li>foreach ($array as $value){</li>
  <li>//Do whatever.</li>
  <li>}</li>
</ul>
<h3>Sorting Arrays</h3>
<table width="300" border="2" cellspacing="0" cellpadding="0">
  <caption>
    Arrays Sorting Functions
  </caption>
  <tr>
    <th scope="col">Function</th>
    <th scope="col">Sorts by</th>
    <th scope="col">Maintains Key Values</th>
  </tr>
  <tr>
    <td>sort()</td>
    <td>values</td>
    <td>no</td>
  </tr>
  <tr>
    <td>rsort()</td>
    <td>values inverse</td>
    <td>no</td>
  </tr>
  <tr>
    <td>asort()</td>
    <td>values</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>arsort()</td>
    <td>values inverse</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>ksort()</td>
    <td>keys</td>
    <td>yes</td>
  </tr>
  <tr>
    <td>krsort()</td>
    <td>keys inverse</td>
    <td>yes</td>
  </tr>
</table>  

<p>extract() function can be used to access array values.</p>
<p>list() function for retrievingvalues from a database.</p>
<p>implode() function turns an array into a string.</p>
<p>explode() function turns a string into an array.</p>
<h3>Creating an array from a form</h3>
<p>Name the Event into this next textfield and choose a day or days.</p>
<form action="/phpwww/ch7/handle_event.php" method="post">
<p>Event Name: <input type="text" name="name" size="30" /></p>
<p>Week Days: 
<input type="checkbox" name="weekdays[]" value="Sunday" /> S 
<input type="checkbox" name="weekdays[]" value="Monday" /> M 
<input type="checkbox" name="weekdays[]" value="Tuesday" /> T 
<input type="checkbox" name="weekdays[]" value="Wednesday" /> W 
<input type="checkbox" name="weekdays[]" value="Thursday" /> T 
<input type="checkbox" name="weekdays[]" value="Friday" /> F 
<input type="checkbox" name="weekdays[]" value="Saturday" /> S 
</p>
<p>
  <input type="submit" name="submit" value="Add the Event!" />
</p>
<br />
</form>



    <div id="clear_footer"></div>
  </div>
  <!-- end #mainContent -->
  <!--start koodi-->
  <div id="koodi">
    <?php
echo '<h2 class="koodi">The Source Code</h2>';
show_source(basename($_SERVER['PHP_SELF'])); //shows the source code of this page
?>
    <!--end koodi-->
  </div>
  <!-- start #footer -->
  <div id="footer">
    <p>&copy; K.Hillerstr&ouml;m 2009<a href="http://validator.w3.org/check?uri=referer">
     <img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Transitional" 
  width="88" height="31" class="footer_img" /></a><a href="http://jigsaw.w3.org/css-validator/check?uri=referer">
   <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!" width="88" height="31"
   class="footer_img"/></a>Last Updated
      <!-- #BeginDate format:Ge1m -->14.10.2009  0:52<!-- #EndDate -->
    </p>
    <!-- end #footer -->
  </div>
  <!-- end #container -->
</div>
</body>
</html>