0% found this document useful (0 votes)
37 views9 pages

Assignment 4 Array (PHP)

The document consists of multiple PHP scripts and HTML forms that demonstrate various array operations such as sorting, inserting, deleting, and merging elements. It includes functionalities for calculating average temperatures, managing stack and queue operations, and manipulating associative arrays. Additionally, it provides options for users to interact with the scripts through HTML forms to perform specific array-related tasks.

Uploaded by

sahilbro8698
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views9 pages

Assignment 4 Array (PHP)

The document consists of multiple PHP scripts and HTML forms that demonstrate various array operations such as sorting, inserting, deleting, and merging elements. It includes functionalities for calculating average temperatures, managing stack and queue operations, and manipulating associative arrays. Additionally, it provides options for users to interact with the scripts through HTML forms to perform specific array-related tasks.

Uploaded by

sahilbro8698
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment 4 Array

//[Link]

<?php

$ht1=array(68, 70, 72, 58, 60, 79, 82, 73, 75, 77, 73, 58, 63, 79, 78, 68, 72, 73, 80, 79,68, 72, 53, 56,
73, 78, 55, 85, 89, 83);

$ht=explode(",",$ht1);

$cnt=count($ht1);

$ttl = 0;

foreach ($ht1 as $h)

$ttl += $h;

$avg = ($ttl / $cnt);

sort($ht1);

echo"<br><b> sorted array:<br>";

print_r($ht1);

echo "<br><b>The average high temperature for the month was $avg &deg;<br><br>";

$topTemps = array_slice($ht1, -5, 5);

echo "<b>*****The warmest five high temperatures were:****** <br>";

foreach ($topTemps as $t)

echo "$t&deg;<br>";

$lowTemps = array_slice($ht1, 0, 5);

echo "*****<b>The coolest five low temperatures were:*****<br>";

foreach ($lowTemps as $l)

echo "$l&deg;<br>";

?>
//[Link]

<html>

<body>

<form method=get action=[Link]>

<center>

<fieldset>

<h1>Choose Menu</h1>

<input type=radio name=op value=1>[Link] an element in stack.<br>

<input type=radio name=op value=2>[Link] an element from stack.<br>

<input type=radio name=op value=3>[Link] the contents of stack.<br>

<input type=radio name=op value=4>[Link] an element in queue.<br>

<input type=radio name=op value=5>[Link] an element from queue.<br>

<input type=radio name=op value=6>[Link] the contents of queue.<br>

<input type=submit name=sb value=Submit>

</fieldset>

</center>

</body>

</html>

//[Link]

<?php

$op=$_GET['op'];

$b=array(20,21,32,43,25,64,72,18,90);

echo"<br> Original array:=<br>";

print_r($b);

switch($op)

case 1:array_push($b,10);

echo"<br>";

echo"After insert an element in stack:<br>";

print_r($b);
break;

case 2:array_pop($b);

echo"<br>";

echo"After delete an element from stack :<br>";

print_r($b);

break;

case 3:echo "Contents of stack.<br>";

print_r($b);

break;

case 4:array_unshift($b,9);

echo"<br>";

echo"After insert an element in queue:<br>";

print_r($b);

break;

case 5: array_shift($b);

echo"<br>";

echo"After delete an element in queue:<br>";

print_r($b);

break;

case 6:echo "Contents of queue<br>";

print_r($b);

break;

?>

//[Link]

<?php

$a = array( 1,2,3,4,5,6);

echo "Original array : =<br>";

foreach ($a as $x)

{
echo "$x\n ";

echo"<br>";

$i= 20;

array_splice( $a, 2, 0, $i );

print_r($a);

echo " <br> After inserting element in the array is :<br>";

foreach ($a as $x)

echo "$x\n ";

echo "<br>";

?>

//[Link]

<?php

$a=array('xyz','bcs','ty');

echo"<br><b> Both arrays is:=</b>";

print_r($a);

$a1=array('ty'=>'xyz','sy'=>'abc','ty'=>'pqr');

echo"<br>";

print_r($a1);

echo"<br><BR><b> by using in_array() function:<br></b>";

if(in_array($a,'xyx'))

echo "<br>The given element is found in defined array ";

else

echo "<br>The given element is not found in defined array ";

}
echo"<br><b> by using array_search() function:<br></b>";

if(array_search('abc',$a1))

echo"<br>\"abc\" found in array";

else

echo"<br>\"abc\"not found in array";

?>

//[Link]

<?php

$array=array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40");

echo "<br><b>Associative array : Ascending order sort by value :=<br></b>";

asort($array);

print_r($array);

echo"<br>";

foreach($array as $y=>$value)

echo $y."=>".$value."<br>";

echo "<br><b>Associative array : Ascending order sort by Key:= <br></b>";

ksort($array);

foreach($array as $y=>$yvalue)

echo $y."=>".$yvalue."<br>";

echo "<br><b>Associative array : Descending order sorting by Value:=<br></b>";

arsort($array);

foreach($array as $y=>$yvalue)

echo $y."=>".$yvalue."<br>";

echo "<br><b>Associative array : Descending order sorting by Key:=<br></b>";


krsort($array);

foreach($array as $y=>$yvalue)

echo $y."=>".$yvalue."<br>";

?>

//[Link]

<html>

<title>Assign4_SETC_Q2</title>

<body>

<center>

<table>

<form method=get action="[Link]">

<fieldset>

[Link] array into chunks<input type=radio name=r value=1><br>

[Link] array by value without changing key<input type=radio name=r value=2><br>

[Link] odd elements<input type=radio name=r value=3><br>

[Link] given array<input type=radio name=r value=4><br>

[Link] intersection<input type=radio name=r value=5></br>

[Link] union<input type=radio name=r value=6><br>

[Link] set difference<input type=radio name=r value=7><br>

<input type=submit name=submit value=go><br>

</fieldset>

</form>

</body>

</html>

//[Link]

<?php

$ch=$_GET['r'];
$a=array("ty"=>25,"sy"=>15,"fy"=>30,"msc"=>3);

echo"<br><b>1st array:<br>";

print_r($a);

$a1=array('ira'=>15,'nia'=>57,'ria'=>3,'alia'=>30);

echo"<br>2nd array:<br>";

print_r($a1);

$a2=array(15,48,10,155,2,78);

echo"<br>3rd array:<br>";

print_r($a2);

echo"<br><br>";

switch($ch)

case 1:

echo"<br>Array in chunks of two <br>";

print_r(array_chunk($a,2,true));

echo"<br><br>";

break;

case 2:echo"<br>without sort:<br>";

print_r($a);

asort($a);//array sort by values

echo"<br>Array in ascending order:<br>";

print_r($a);

arsort($a);//reverse array sort by values

echo"<br>Array in desecending order:<br>";

print_r($a);

break;

case 3:

function isodd($v)

return $v%2;

}
print_r($a2);

echo "<br>";

echo"<br>Filter the odd elements from an array.<br>";

print_r(array_filter($a2,'isodd'));

break;

case 4 :

echo "<br>1st array<br>";

print_r($a);

echo "<br>2nd array<br>";

print_r($a1);

echo "<br>Array Merge of both array:<br>";

print_r(array_merge($a,$a1));

break;

case 5:

echo "<br>1st array<br>";

print_r($a);

echo "<br>2nd array<br>";

print_r($a1);

echo "<br>Array Intersection of both array:<br>";

print_r(array_intersect($a,$a1));

break;

case 6:

echo "<br>1st array<br>";

print_r($a);

echo "<br>2nd array<br>";

print_r($a1);

echo "<br>Array Union of both array:<br>";

$union=array_merge($a,$a1);

print_r(array_unique($union));

break;

case 7:
echo "<br>1st array<br>";

print_r($a);

echo "<br>2nd array<br>";

print_r($a1);

echo "<br>Array Differnce of both array:<br>";

print_r(array_diff($a,$a1));

break;

You might also like