In: Computer Science
1. Create a PHP program containing an multi dimensional array of all the first and last names of the students in your class. Sort the array by last name in alphabetic order. Also sort the array in reverse order.
2. Split the array from #1 into two arrays; one containing first names, the other containing last names.
1. Create a PHP program containing an multi dimensional array of all the first and last names of the students in your class. Sort the array by last name in alphabetic order. Also sort the array in reverse order.
<!DOCTYPE html>
<html>
<body>
<?php
$age = array("Peter"=>"35", "Ben"=>"37",
"Joe"=>"43");
asort($age);
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
</body>
</html>
2.Split the array from #1 into two arrays; one containing first names, the other containing last names.
<?php
// Simple PHP program to find subarrays
// whose averages are equal
// Finding two subarrays
// with equal average.
function findSubarrays( $arr, $n)
{
$found = false;
$lsum = 0;
for ( $i = 0; $i < $n - 1; $i++)
{
$lsum += $arr[$i];
$rsum = 0;
for ( $j = $i + 1; $j < $n; $j++)
$rsum += $arr[$j];
// If averages of arr[0...i] and
// arr[i+1..n-1] are same. To avoid
// floating point problems we compare
// "lsum*(n-i+1)" and "rsum*(i+1)"
// instead of "lsum/(i+1)" and "rsum/(n-i+1)"
if ($lsum * ($n - $i - 1) ==
$rsum * ($i + 1))
{
echo "From ( 0 ", $i," )".
" to (", $i + 1," ", $n - 1,")\n";
$found = true;
}
}
// If no subarrays found
if ($found == false)
echo "Subarrays not found" ;
}
// Driver code
$arr = array(1, 5, 7, 2, 0);
$n = count($arr);
findSubarrays($arr, $n);
// This code is contributed by vt_m
?