Source code for:
challenge1_5.php
<HTML>
<HEAD>
<TITLE>Q7310 Challenge1.5 Advanced Arrays Processing </TITLE>
</HEAD>
<BODY bgcolor="000000" link="#FFFF00" vlink="#FFFF00" alink="#00FF00" text="#ececec">
<H1 align="center">
Program #5 <BR> MANIPULATING ARRAYS
</H1>
<DIV align="centerTABLE width"> <FONT face="arial, helvetica, sans-serif" size=2 color="ececec">
<TABLE width="90%" border=1 cellpadding=10 cellspacing=0>
<TR>
<TD>
<H2>
<P align="center"> This is my <B>OPTIONAL program </B> </P>
</H2>
<FONT face="arial, helvetica, sans-serif" size=2 color="ececec">
<B> Challenge 5:</B>
<UL>
<LI> 1. Write a program that manipulates an array using the
various array functions. </LI>
<LI> 2. Start the program with an empty array - no values.
</LI>
<LI> 3. Use push, pop, shift, and count. </LI>
<LI> 4. First, push at least 25 items onto the array - it
can be numbers, text, whatever you want. </LI>
<LI> 5. Print out the contents of the array using a loop.
</LI>
<LI> 6. Second, pop off the last 5 elements of the array.
</LI>
<LI> 7. Print the array using a different type of loop. </LI>
<LI> 8. Third, use shift to remove the first 5 elements. </LI>
<LI> 9. Print the array. </LI>
<LI> 10. Finally, use count to determine the final size of
the array and print it. </LI>
<LI> 11. Include comments. </LI>
</UL>
</FONT> </TD>
</TR>
<TR>
<TD>
<?php
// INITIALIZE PHONE DIRECTORY ARRAY
$character = array (
array (
"name" => "C.Mack",
"sport" => "WBB",
"course" => "Mentoring",
"time" => "Su 7 pm",
"phone" => "573-999-9999"
),
array (
"name" => "C.S.James",
"sport" => "WBB",
"course" => "Mentoring, Ag Econ & Educ",
"time" => "Su 7 pm, M W11 am, T 10:15 am, R 8:45 am ",
"phone" => "573-999-9999"
),
array (
"name" => "K.Anderson",
"sport" => "Gymnastics",
"course" => "Acctg 36",
"time" => "M 9 am",
"phone" => "573-999-9999"
),
array (
"name" => "S.Simons",
"sport" => "WSB",
"course" => "C&IV",
"time" => "M 10:15 am",
"phone" => "573-999-9999"
),
array (
"name" => "J.Scott",
"sport" => "FB",
"course" => "Personal Finance",
"time" => "M 11 am",
"phone" => "573-999-9999"
),
array (
"name" => "S.Paffrath",
"sport" => "FB",
"course" => "Ag Econ",
"time" => "T 10:15 am",
"phone" => "573-999-9999"
),
array (
"name" => "J.Humprey",
"sport" => "FB",
"course" => "C&IV",
"time" => "T 1:15 pm",
"phone" => "573-999-9999"
),
array (
"name" => "E.Goldsmith",
"sport" => "FB",
"course" => "C&IV",
"time" => "W 11 am",
"phone" => "573-999-9999"
),
array (
"name" => "M.Rucker",
"sport" => "FB",
"course" => "Ag Computing",
"time" => "W 11:45 am",
"phone" => "573-999-9999"
),
array (
"name" => "T.Omboga",
"sport" => "FB",
"course" => "Ag Econ",
"time" => "W 12:30 pm",
"phone" => "573-999-9999"
),
array (
"name" => "A.Britt",
"sport" => "FB",
"course" => "Ag Econ",
"time" => "W 1:15 pm",
"phone" => "573-999-9999"
),
array (
"name" => "T.Woodley",
"sport" => "WR",
"course" => "Ag Econ",
"time" => "T R 1:30 pm",
"phone" => "573-999-9999"
),
array (
"name" => "L.E. Grice",
"sport" => "Soccer",
"course" => "Statistics",
"time" => "R am",
"phone" => "573-999-9999"
),
array (
"name" => "__________",
"sport" => "________",
"course" => "Open Session",
"time" => "R F by appointment",
"phone" => "573-999-9999"
),
);
?>
<?php
// =================== PUSH, POP, & SHIFT ======================
?>
<FONT color="gold">
<H2 align="center">My <B>Array</B> Push, Pop, & Shift Demonstration
</H2>
</FONT> <FONT face="arial, helvetica, sans-serif" size=2 color="ececec">
<?php
//==================== PUSH ====================================
// INITIALIZE VARIABLES
echo "<h2> PUSH data into an Array; Steps 1 - 5; 10-11 <BR> </h2>";
settype($stack, 'array');
echo "Verify <i> \$stack </i> is an <b>";
print gettype($stack); echo "</b>";
$r=1;
$c=0;
// PUSH &character[$name] and $r INTO ARRAY, $Stack
foreach ( $character as $pArray )
{
foreach ( $pArray as $pIndex=>$pLine )
{
IF ($c==0) {
array_push($stack, $r, $pLine);
}
$c=$c+1;
}
// print "<br>";
$r=$r+1; //increment row counter
$c=0; //re-initialize "column" or field indicator
}
?>
</FONT> <FONT face="arial, helvetica, sans-serif" size=2 color="ececec">
<?php
// COUNT THE ELEMENTS IN THE NEW ARRAY, $STACK
$counter = count($stack);
print " and deterime the number of elements in <i> \$stack </i> is $counter ";
// PRINT ROUTINE
echo "<TABLE width='50%' border=1> ";
print "<TR> <TD width='20%' align='center'> Index </TD>";
print "<TD align='center'> Value </TD> </TR>";
FOR ($x=0; $x < $counter; ++$x) {
print "<TR> <TD> $x: </TD> <TD> $stack[$x] </TD> </TR>";
}
echo "</TABLE>";
?>
</FONT>
<HR>
<?php
// ========================= POP =========================
// DELETE "ARARY_POP" LAST FIVE (5) ELEMENTS
// INITIALIZE VARIABLES
echo "<br> <h2> POP, remove last array elements; Steps 6 - 7; 10-11 <br> </h2>";
settype($dump, 'array');
echo "Verify <i> \$dump </i> is an <b>";
print gettype($dump); echo " </b> ";
$x=5;
$i = 0;
$n = count($stack);
$n -=1;
print " Beginning LastIndex: $n"; print " BegLastValue: $stack[$n] <br>";
echo "<BLOCKQUOTE>";
// DELETE "POP" ELEMENTS
while ($i < $x) {
print "<br> Iteration: "; echo $i++ ;
$n -=1;
print " \$n = $n";
print " Index: $n"; print " Value: $stack[$n] NewTotalElements: ";
$dump = array_pop($stack);
print count($stack); echo "<br>";
}
echo "</BLOCKQUOTE>";
// VERIFY LAST ITEMS OF ARRAY, $stack
$counter = count($stack) - 1;
print " LastIndex: $counter LastValue: $stack[$counter] <br>";
$x = count($stack);
echo "<BLOCKQUOTE>";
$i = 0;
WHILE ($i < $x) {
print " $i -- $stack[$i] <br>";
$i++;
};
echo "</BLOCKQUOTE>";
?>
<HR>
<?php
// ===================== SHIFT =============================
// DELETE "ARARY_SHIFT" FIRST FIVE (5) ELEMENTS
// INITIALIZE VARIABLES
echo "<br> <h2> SHIFT, remove, beginning elements; Steps 8 - 11 <br> </h2>";
settype($dump, 'array');
$x=5;
$i = 0;
$n = count($stack);
$n -=1;
print " BeginningFirstIndex: $n"; print " BegFirstValue: $stack[0] <br>";
echo "<BLOCKQUOTE>";
// DELETE "SHIFT" ELEMENTS
while ($i < $x) {
print "<br> Iteration: "; echo $i++ ;
$n -=1;
print " \$n = $n";
print " Index: $n"; print " Value: $stack[0] NewTotalElements: ";
$dump = array_shift($stack);
print count($stack); echo "<br>";
}
echo "</BLOCKQUOTE>";
// VERIFY FIRST ITEMS OF ARRAY, $stack
$counter = count($stack) - 1;
print " LastIndex: $counter NewFIRSTValue: $stack[0] <br>";
$x = count($stack);
echo "<BLOCKQUOTE>";
// do {
// PRINT "Increment: $i Value: $stack[$i] <br>";
// } while ($i < $x);
$i = 0;
WHILE ($i < $x) {
print " $i -- $stack[$i] <br>";
$i++;
};
echo "</BLOCKQUOTE>";
?>
</TD>
</TR>
<TR>
<TD>
<?php
//----------------------------- PAGE FOOTER INFORMATION -----------
?>
</TD>
</TR>
<TR>
<TD> SOURCE CODE: Click <A href="http://rrchubbard.org/php/q7310/source1_5.php" target="_blank">
HERE </A> </TD>
</TR>
</TABLE>
</FONT> </DIV>
[<A href="top" target="_self">Top</A>] <BR>
Last Updated <I>:
<!-- #BeginDate format:fcAm1m -->Friday, October 15, 2004 19:36<!-- #EndDate -->
</I>
<P>
<FONT face="Arial, Helvetica, sans-serif" size="1">
(c) 2004 Robin Y. Mabry Hubbard <A href="mailto:4ascii@marz.com">4ascii@marz.com</A>
</FONT>
</P>
</BODY>
</HTML>
|