Web Technologies
Slip29
Q.1) 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.
6. Find the intersection of two arrays.
7. Find the union of two arrays.
8. Find set difference of two arrays [Marks30]
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.
6. Find the intersection of two arrays.
7. Find the union of two arrays.
8. Find set difference of two arrays [Marks30]
Solution
a.php
<pre><?php
$op=$_POST['op'];
switch($op)
{
case 1:
$a=array('y'=>10,'a'=>6,'e'=>3,'f'=>12,'m'=>2);
echo"Before sort :<br>";
print_r($a);
sort($a);//Ascending
echo"Ascending sort :<br>";
print_r($a);
$a=array('y'=>10,'a'=>6,'e'=>3,'f'=>12,'m'=>2);
rsort($a);
echo"Descending sort :<br>";
print_r($a);
break;
case 2:
$a=array('y'=>10,'a'=>6,'e'=>3,'f'=>12,'m'=>2);
echo"Before sort :<br>";
print_r($a);
asort($a);
echo"Ascending sort :<br>";
print_r($a);
$a=array('y'=>10,'a'=>6,'e'=>3,'f'=>12,'m'=>2);
arsort($a);
echo"Descending sort :<br>";
print_r($a);
break;
case 3:
$a=array(5,1,3,8,15,6,4);
echo"Before Filter :<br>";
print_r($a);
$b=array_filter($a,'is_odd');
echo"After Filter:<br>";
print_r($b);
break;
case 4:
$a=array(1,3,3,1,5,5,2,3,3,1,5);
$b=array(8,7,3,2,6,4,1,9,4,9,2);
array_multisort($a,$b);
for($i=0;$i<count($a);$i++)
echo $a[$i].' '.$b[$i].'<br>';
break;
case 5:
$a=array(5,1,3,8,15,6,4);
$b=array(4,9,5,10,7,3,2);
$c=array_merge($a,$b);
echo "Array 1:";
print_r($a);
echo "Array 2:";
print_r($b);
echo "Merged Array:";
print_r($c);
break;
case 6:
$a=array(5,1,3,8,15,6,4);
$b=array(4,9,5,10,7,3,2);
$c=array_intersect($a,$b);
echo "Array 1:";
print_r($a);
echo "Array 2:";
print_r($b);
echo "Intersection of two Arrays:";
print_r($c);
break;
case 7:
$a=array(5,1,3,8,15,6,4);
$b=array(4,9,5,10,7,3,2);
$c=array_merge($a,$b);
$c=array_unique($c);
echo "Array 1:";
print_r($a);
echo "Array 2:";
print_r($b);
echo "Union of two Arrays:";
print_r($c);
break;
case 8:
$a=array(5,1,3,8,15,6,4);
$b=array(4,9,5,10,7,3,2);
$c=array_diff($a,$b);
echo "Array 1:";
print_r($a);
echo "Array 2:";
print_r($b);
echo "Difference of two Arrays:";
print_r($c);
break;
}
function is_odd($n)
{
return $n%2==1;
}
?>
</pre>
b.html
<form method='post' action='a.php'>Select Option :<br>
<select name='op'>
<option value=1>Sort the array by values in ascending & descending order</option>
<option value=2>Sort the array by values without changing keys</option>
<option value=3>Filter the odd elements from an array</option>
<option value=4>Sort the different array at a glance </option>
<option value=5>Merge the given arrays</option>
<option value=6>Find the intersection of two arrays</option>
<option value=7>Find the union of two arrays</option>
<option value=8>Find the difference of two arrays</option>
</select>
<br>
<input type='submit'>
<input type='reset'>
</form>
Tags:
Web Technologies