Martha's CIS 52 Lab #2 Number Exercise #1

Lab #2 Exercise #1.  Create a string of names consisting of "John, Jerry, Ann, Sanji, Wen, Paul, Louise, Peter".

  1. Create a single dimensional array out of the string.
  2. Get the total number of values in the array
  3. Search for a specific value in the array.  You can determine what that value is.
  4. Sort the array by keys in descending order
  5. After sorting the array in (d), continue to sort the array by value in ascending order
  6. After sorting the array in (e), continue to sort the array by value in reverse order.
  7. Remove the first element from the array.
  8. Add "Willie" and "Daniel" to the end of the array.
  9. Replace "Paul" with "Andre".
  10. Add "Alisha" to the beginning of the array.
  11. Output the minimum and maximum value of the array
  12. Generate an output that returns the current element key and value and moves the internal pointer forward.
  13. Randomly retrieve a value from the array
  14. Create another array of names.  Choose names of your choice.
  15. Merge both of your arrays together and display them in sorted order.

**************************************************************

CODE:

<?php
/* THESE are the INSTRUCTIONS: */
// Create a string of names consisting of "John, Jerry, Ann, Sanji, Wen, Paul, Louise, Peter".
echo "<b>A. Create a single dimensional array out of the string of names consisting of \"John, Jerry, Ann, Sanji, Wen, Paul, Louise, Peter\": </b>";
$names = array("John", "Jerry", "Ann", "Sanji", "Wen", "Paul", "Louise", "Peter");
echo "<br /><b>&nbsp;&nbsp; 1) My names array with a <i>for()</i> loop:</b> ";
for ($i=0;$i<=6;$i++){
echo $names[$i].", ";
}
echo $names[count($names) - 1].".";
echo "<br />&nbsp;&nbsp;<b> 2) My names array with <i>print_r()</i>:</b> ";
print_r($names);
echo "<br /><b>&nbsp;&nbsp; 3) My array names with <i>foreach(".'$names as $value'.")</i>:</b> ";
foreach ($names as $value) {
echo $value."; ";}
echo "<br /><b>B. Get the total number of values in the array, using <i>sizeof()</i>: </b>".sizeof($names);
echo "<br /><b>C. Search for a specific value in the array using <i>array_search()</i>; Sanji is at position: </b>";
echo array_search('Sanji', $names);
echo "<br /><b>D. Sort the array by keys in descending order, using <i>ksort()</i>: </b><br />";
ksort($names);
print_r($names);
echo "<br /><b>E. After sorting the array in (d), continue to sort the array by value in ascending order, using <i>sort()</i></b>:<br /> ";
sort($names);
print_r($names);
echo "<br /><b>F. After sorting the array in (e), continue to sort the array in reverse order, using rsort(): </b><br />";
rsort($names);
print_r($names);
// G. Remove the first element from the array.
$namesShifted = array_shift($names);
echo "<br /><b>G. Remove the first element with <i>array_shift()</i> , printing with <i>print_r()</i>:</b><br />";
print_r($names);
// ASSISTANCE from: http://phpbuilder.com/manual/en/book.array.php
echo "<br /><b>H. Add \"Willie\" and \"Daniel\" to the end of the array, using <i>array_push()</i></b>:<br />";
array_push($names, "Willie", "Daniel");
print_r($names);
echo "<br /><b>I. Replace \"Paul\" with \"Andre\", using <i>array_splice()</i> and print_r():</b><br />";
array_splice($names, 2, 1, "Andre");
print_r($names);
echo "<br /><b>J. Add \"Alisha\" to the beginning of the array, using <i>array_unshift()</i>:</b><br />";
array_unshift($names, "Alisha");
print_r($names);

echo "<br /><b>K. Output the minimum and maximum value of the array, using <i>min()</i> and <i>max()</i>:</b><br />";
var_dump(min($names));
echo "<br />";
var_dump(max($names));
echo "<br />How odd...these names are the first and last of the key/value pairs...";
echo "<br /><b>L. Generate an output that returns the current element key and value and moves the internal pointer forward.</b><br />";
while (list($key, $val) = each($names))
{
echo "$key => $val<br />";
}
echo "<b>M. Randomly retrieve a value from the array</b>:<br />";
$rand_keys = array_rand($names, 2);
echo $names[$rand_keys[0]]." is the first random name.<br />";
echo $names[$rand_keys[1]]." is another random name.";
echo "<br /><b>N. Create another array of names. Choose names of your choice</b>:<br />";
$neighbors = array("Paul", "Maureen", "Kate", "Sofia", "Kevin", "Mary");
print_r($neighbors);
echo "<br /><b>O. Merge both of your arrays together:</b><br />";
$newArray = array_merge($names, $neighbors);
print_r($newArray);
echo "<br /><b>...and display them in sorted order:</b><br />";
sort($newArray);
print_r($newArray);
/* END of INSTRUCTIONS */
?>

 

**************************************************************

ANSWER:

A. Create a single dimensional array out of the string of names consisting of "John, Jerry, Ann, Sanji, Wen, Paul, Louise, Peter":
   1) My names array with a for() loop: John, Jerry, Ann, Sanji, Wen, Paul, Louise, Peter.
   2) My names array with print_r(): Array ( [0] => John [1] => Jerry [2] => Ann [3] => Sanji [4] => Wen [5] => Paul [6] => Louise [7] => Peter )
   3) My array names with foreach($names as $value): John; Jerry; Ann; Sanji; Wen; Paul; Louise; Peter;
B. Get the total number of values in the array, using sizeof(): 8
C. Search for a specific value in the array using array_search(); Sanji is at position: 3
D. Sort the array by keys in descending order, using ksort():
Array ( [0] => John [1] => Jerry [2] => Ann [3] => Sanji [4] => Wen [5] => Paul [6] => Louise [7] => Peter )
E. After sorting the array in (d), continue to sort the array by value in ascending order, using sort():
Array ( [0] => Ann [1] => Jerry [2] => John [3] => Louise [4] => Paul [5] => Peter [6] => Sanji [7] => Wen )
F. After sorting the array in (e), continue to sort the array in reverse order, using rsort():
Array ( [0] => Wen [1] => Sanji [2] => Peter [3] => Paul [4] => Louise [5] => John [6] => Jerry [7] => Ann )
G. Remove the first element with array_shift() , printing with print_r():
Array ( [0] => Sanji [1] => Peter [2] => Paul [3] => Louise [4] => John [5] => Jerry [6] => Ann )
H. Add "Willie" and "Daniel" to the end of the array, using array_push():
Array ( [0] => Sanji [1] => Peter [2] => Paul [3] => Louise [4] => John [5] => Jerry [6] => Ann [7] => Willie [8] => Daniel )
I. Replace "Paul" with "Andre", using array_splice() and print_r():
Array ( [0] => Sanji [1] => Peter [2] => Andre [3] => Louise [4] => John [5] => Jerry [6] => Ann [7] => Willie [8] => Daniel )
J. Add "Alisha" to the beginning of the array, using array_unshift():
Array ( [0] => Alisha [1] => Sanji [2] => Peter [3] => Andre [4] => Louise [5] => John [6] => Jerry [7] => Ann [8] => Willie [9] => Daniel )
K. Output the minimum and maximum value of the array, using min() and max():
string(6) "Alisha"
string(6) "Willie"
How odd...these names are the first and last of the key/value pairs...
L. Generate an output that returns the current element key and value and moves the internal pointer forward.
0 => Alisha
1 => Sanji
2 => Peter
3 => Andre
4 => Louise
5 => John
6 => Jerry
7 => Ann
8 => Willie
9 => Daniel
M. Randomly retrieve a value from the array:
Louise is the first random name.
Ann is another random name.
N. Create another array of names. Choose names of your choice:
Array ( [0] => Paul [1] => Maureen [2] => Kate [3] => Sofia [4] => Kevin [5] => Mary )
O. Merge both of your arrays together:
Array ( [0] => Alisha [1] => Sanji [2] => Peter [3] => Andre [4] => Louise [5] => John [6] => Jerry [7] => Ann [8] => Willie [9] => Daniel [10] => Paul [11] => Maureen [12] => Kate [13] => Sofia [14] => Kevin [15] => Mary )
...and display them in sorted order:
Array ( [0] => Alisha [1] => Andre [2] => Ann [3] => Daniel [4] => Jerry [5] => John [6] => Kate [7] => Kevin [8] => Louise [9] => Mary [10] => Maureen [11] => Paul [12] => Peter [13] => Sanji [14] => Sofia [15] => Willie )