Write a menu
driven program in PHP to perform the following operations on associative arrays
1. Sort the array by values (changing the keys) in ascending, descending order.
2. Also sort the array by values without changing the keys.
3. Filter the odd elements from an array.
4. Sort the different arrays at a glance using single function.
5. Merge the given arrays.
6Find the intersection of two arrays.
7. Find the union of two arrays.
8. Find set difference of two arrays.
Slip8.html
<html>
<body>
<form method="post" action="slip8.php">
Please check the following options to Perform the operations :
<p><input type="radio" name="operation" value="1">1 Ascending order /decending order</p>
<p><input type="radio" name="operation" value="2">2 Sort</p>
<p><input type="radio" name="operation" value="3">3 Filter</p>
<!--<p><input type="radio" name="operation" value="4">4 diffrent array sort</p>à
<p><input type="radio" name="operation" value="5">5 Merge</p>
<p><input type="radio" name="operation" value="6">6 Intersection</p>
<p><input type="radio" name="operation" value="7">7 UNION</p>
<p><input type="radio" name="operation" value="8">8 DiFFERENCE</p>
<!--<p><input type="text" name="element_vaue" value=""/>-->
<p><input type="submit" name="submit" value="Submit"></p>
</form>
</body>
</html>
slip8.php
<?php
/*Define array*/
$arr1 = array('a','b','c');
$arr2= array('b','d','e');
$fruits = array("d"=>"lemon", "a"=>"orange", "b"=>"banana", "c"=>"apple");
$operation = $_POST['operation'];
switch($operation)
{
case'1':
echo"Original Array Elements : <PRE>";
print_r($fruits);echo"</PRE>";
ksort($fruits);
echo" <font color=red> KSort </font><PRE>";
foreach ($fruits as $key => $val)
{
echo "$key = $val\n";
}
krsort($fruits);
echo" <font color=red> KRSort </font><PRE>";
foreach ($fruits as $key => $val)
{
echo "$key = $val\n";
}
break;
case'2':
echo"Original Array Elements : <PRE>";
print_r($fruits);echo"</PRE>";
sort($fruits);
echo" <font color=red> kSort </font><PRE>";
foreach ($fruits as $key => $val)
{
echo "$key = $val\n";
}
break;
Case'3':
echo"Original Array Elements : <PRE>";
function test_odd($var)
{
return($var & 1);
}
$a1=array(1,2,3,4);
print_r($a1);echo"</PRE>";
echo" <font color=red> filter </font><PRE>";
print_r(array_filter($a1,"test_odd"));
break;
case'5':
echo"Original Array Elements : <PRE>";
print_r($arr1);echo"</PRE>";
print_r($arr2);echo"</br>";
$mer=array_merge($arr1,$arr2);
echo" <font color=red> Merge </font><PRE>";
print_r($mer);echo"</br>";
break;
case'6':
echo"Original Array Elements : <PRE>";
print_r($arr1);echo"</PRE>";
print_r($arr2);echo"</br>";
$interset=array_intersect($arr1,$arr2);
echo" <font color=red> Intersection </font><PRE>";
print_r($interset);echo"</br>";
break;
case'7':
echo"Original Array Elements : <PRE>";
print_r($arr1);echo"</PRE>";
print_r($arr2);echo"</br>";
$uni=array_unique(array_merge($arr1,$arr2));
echo" <font color=red>>UNION</font><br>";
print_r($uni);echo"</br>";
break;
case'8':
echo"Original Array Elements : <PRE>";
print_r($arr1);echo"</PRE>";
print_r($arr2);echo"</PRE>";
$diff=array_diff($arr1,$arr2);
echo"DiFFERENCE<br>";
print_r($diff);echo"</br>";
break;
}
?>