Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exercise 1 The following set of short questions will focus on getting you famili

ID: 3705996 • Letter: E

Question

Exercise 1 The following set of short questions will focus on getting you familiar with how to write PHP functions as well as how to make use of the pre-existing PHP functions. All your PHP functions must be declared in the document body section and each functions name must be as specified below. To demonstrate the functionality of each method, you must make function calls in the document body. Include a heading (hl h6) that indicates which function is being tested before each function demonstration. The use of Global Variables is forbidden! Test all your functions with some relevant input parameter values. with some relevant input parameter

Explanation / Answer

Below is the solution:

code:

<?php
$code=1; //maunally declare the code value either 1,2,3,4 or anything else othar than that value will print wrong.
$Array=array("Jack"=>"55","Anita"=>"30","Ramesh"=>"40","Sophia"=>"21","Nastran"=>"41","William"=>"39","David"=>"5");

if($code==1){ //if code =1
   asort($Array); //asort() function that sort the array by ascending order sort by value
   function sortHash2($Array,$code){ //function
       echo "Ascending order sort by value. <br>";
       foreach($Array as $key=>$key_value) //print the array after sort
       {
           echo "Key=" . $key . ", Value=" . $key_value; //print key with value
           echo "<br>";
       }
   }  
}
else if($code==2){ //if code =2
   ksort($Array); //ksort() function that sort the array by descending order sort by key
   function sortHash2($Array,$code){
       echo "Ascending order sort by key. <br>";
       foreach($Array as $key=>$key_value) //print the array after sort
       {
           echo "Key=" . $key . ", Value=" . $key_value; //print key with value
           echo "<br>";
       }
   }  
}

else if($code==3){ //if code =3
   arsort($Array); //arsort() function that sort the array by descending order sort by Value
   function sortHash2($Array,$code){
       echo "Descending order sort by value. <br>";
       foreach($Array as $key=>$key_value) //print the array after sort
       {
           echo "Key=" . $key . ", Value=" . $key_value; //print key with value
           echo "<br>";
       }
   }  
}

else if($code==4){ //if code =4
   krsort($Array); //arsort() function that sort the array by descending order sort by key
   function sortHash2($Array,$code){
       echo "Descending order sort by key. <br>";
       foreach($Array as $key=>$key_value) //print the array after sort
       {
           echo "Key=" . $key . ", Value=" . $key_value; //print key with value
           echo "<br>";
       }
   }  
}

else{
   echo "Wrong Code."; //prints wrong code and exit the statement
   exit();
}


sortHash2($Array,$code); //create a function sortHash2 and pass the the array and the code value

?>

sample output:

Ascending order sort by value.
Key=David, Value=5
Key=Sophia, Value=21
Key=Anita, Value=30
Key=William, Value=39
Key=Ramesh, Value=40
Key=Nastran, Value=41
Key=Jack, Value=55

Ascending order sort by key.
Key=Anita, Value=30
Key=David, Value=5
Key=Jack, Value=55
Key=Nastran, Value=41
Key=Ramesh, Value=40
Key=Sophia, Value=21
Key=William, Value=39

Descending order sort by value.
Key=Jack, Value=55
Key=Nastran, Value=41
Key=Ramesh, Value=40
Key=William, Value=39
Key=Anita, Value=30
Key=Sophia, Value=21
Key=David, Value=5


Descending order sort by key.
Key=William, Value=39
Key=Sophia, Value=21
Key=Ramesh, Value=40
Key=Nastran, Value=41
Key=Jack, Value=55
Key=David, Value=5
Key=Anita, Value=30