Lab #2 Exercise #1. Create a string of names consisting of "John, Jerry, Ann, Sanji, Wen, Paul, Louise, Peter".
**************************************************************
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> 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 /> <b> 2) My names array with <i>print_r()</i>:</b> ";
print_r($names);
echo "<br /><b> 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":