hello i need help with these php functions , using local variables only. F. Func
ID: 3707197 • Letter: H
Question
hello i need help with these php functions , using local variables only.
F. Function: findUniqueandSort ,Parameter(s): Array of integer numbers
Scan the entire array and print out the array excluding any duplicate elements, then sort the array. Example: input Array a = 1,3,2,1,3, 1 would output 1,2,3.
G. Function: sortHash1 ,Parameter(s): Associative Array ,
Given an associative array using full-names as keys and annual salaries as values; in a table format, print out the name and corresponding salary of each individual in increasing order (from lowest salary to the highest).
H. Function: sortHash2, Parameter(s): Associative Array,
code Write a PHP function to accept an associative array (as follows) and a code as input parameters, and then sort the input associative array as follows. Example of the input array: array("Jack"=>"55", "Anita"=>"30","Ramesh"=>"40","Sophia"=>"21","Nastran"=>"41","William"=>"39","David"=>"5") in
a) if code =1: ascending order sort by value
b) if code =2: ascending order sort by Key
c) if code =3: descending order sorting by Value d) if code =4: descending order sorting by Key
e) For other values of code: Function only prints ( “Wrong Code”)
I. Function: averageTemp, Parameter(s): Associative Array
Write a PHP function to calculate and display average temperature, and four lowest and highest temperatures. Recorded temperatures are : [78, 60, 62, 68, 71, -17, 52, 68, 73, 85, 66, 64, 76, 63, 75, 76, 73, 68, 62, 73, -10, 72, 65, 80,74, 62, 62, 65, 64, 0, 68, 73, 75, 79, 73, 77]. Expected Outputs :
Average Temperature is : Average Value List of four lowest temperatures : -17, -10, 0, 52 .List of four highest temperatures :78, 79, 80,85
J. Function: findatStartorEnd Parameter(s): word, string
Write a PHP function to test if a given word occurs at the beginning or at the end of a given string. If yes, it returns: True, otherwise it returns: False
Example for: “I love PHP”, “PHP” --> function returns : True
Example for: “I love PHP and C++”, “PHP” --> function returns : False
Explanation / Answer
<?php
$data = ['1','6','7','8','2'];
$assoArray = ['test'=>9,'test1'=>5];
findUniqueandSort($data);
sorthash1($assoArray);
temparature($data);
function findUniqueandSort($inputArray)
{
$unique =array_unique($inputArray);
sort($unique);
$clength = count($unique);
$data = [];
for($x = 0; $x < $clength; $x++) {
$data[]= $unique[$x];
}
print_r($data);
}
function sorthash1($inputArray)
{
asort($inputArray);
print_r($inputArray);
}
function applysort($inputArray,$code)
{
if($code == 1)
{
asort($inputArray);
print_r($inputArray);
}
if($code == 2)
{
rsort($inputArray);
print_r($inputArray);
}
if($code == 3)
{
ksort($inputArray);
print_r($inputArray);
}
if($code == 4)
{
print_r("wrong data");
}
}
function temparature($inputArray)
{
$data = $inputArray;
$data1 =$inputArray;
asort($data);
rsort($data1);
for ($i=0; $i < 3; $i++)
{
echo "Lowest value"+$i+':'+$data[$i];
}
for ($i=0; $i < 3; $i++)
{
echo "Highest value"+$i+':'+$data1[$i];
}
}
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.