1. Write a PHP script to sort the following associative array : array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40") in a) ascending order sort by value b) ascending order sort by Key c) descending order sorting by Value d) descending order sorting by Key
HTML CODE:
<html>
<head>
<style>
html
{
background-color:lightpink
}
</style>
</head>
<body>
<form action="c1.php"method="GET">
<p><b> Which operation do you want to perform? </b></p>
<input type="radio" name="op" value="1">
<i> <label for="ascV">ascending order sort by value<label></i><br><br>
**********************************************************************************************************************
<br>
<input type="radio" name="op" value="2">
<i> <label for="ascK">ascending order sort by key</label></i><br><br>
**********************************************************************************************************************
<br>
<input type="radio" name="op" value="3">
<i> <label for="desV">descending order sort by value</label></i><br><br>
**********************************************************************************************************************
<br>
<input type="radio" name="op" value="4">
<i> <label for="desK">descending order sort by key<label></i><br><br>
**********************************************************************************************************************
<br>
<br>
<input type="submit" value="submit">
</form>
</body>
</html>
PHP CODE:
<?php
$ch=$_GET['op'];
$arr=array("Sophia"=>31,"Jacob"=>41,"William"=>39,"Ramesh"=>40);
switch($ch)
{
case 1 : $p=asort($arr);
echo"Ascending order sort by value";
print_r($arr);
break;
case 2 : $p=ksort($arr);
echo"Ascending order sort by key";
print_r($arr);
break;
case 3 : $p=arsort($arr);
echo"Descending order sort by value";
print_r($arr);
break;
case 4 : $p=krsort($arr);
echo"Descending order sort by key";
print_r($arr);
break;
}
?>
Comments
Post a Comment