You are on page 1of 3

TKF2083 Web Programming

Lab Guide 4 Manipulating Array

Assignment 1: Remove Duplicate Element

<html>
<head>
<title>Title here!</title>
</head>
<body>
<?php
$student= array("Muhammad", "Idris", "Ahmad", "Zakaria",
"Ayub", "Muhammad", "Zakaria", "Yunus", "Yusof");

echo "<h2> List of student (original)</h2>";


for($i=0; $i< count($student); ++$i)
echo "{$student[$i]}<br />\n";

//remove duplicate Element


$student = array_unique($student);
$student = array_values($student);

echo "<h2> List of student (Afer Remove Duplicate)</h2>";


for($i=0; $i< count($student); ++$i)
echo "{$student[$i]}<br />\n";
?>
</body>
</html>

Assignment 2: Comparing Arrays


a. array_diff() function returns an array of elements that exist in one array but not in any arrays
to which it is compared.
b. array_intersec() function returns an array of the elements that are common to all the arrays
that are compared.

Step 1 : Write the following form and save as compareForm.php

<html>
<head>
<title>Title here!</title>
</head>
<body>
<form name="f1" method="POST" action="compareProcess.php">
<?php
for($i=1; $i<=10; $i++)
{
echo "<p>Top 10 in Area $i: <input type='text' name='area[$i]'
size='10'></p>";

Page 1 of 3
}

for($i=1; $i<=10; $i++)


{
echo "<p>Top 10 in Population $i: <input type='text'
name='population[$i]' size='10'></p>";

?>
<input type="submit" name="hantar" value="Different">
<input type="submit" name="hantar" value="Intersection">
<input type="reset" name="reset" value="Cancel">
</form>
</body>
</html>

Step 2 : Write the following php to compare two arrays (area and population) and save as
compareProcess.php

<html>
<head>
<title>Top 10 Area and Population</title>
</head>
<body>
<?php
//get data from form
$areaArray= $_POST['area'];

$populationArray= $_POST['population'];

//display the value from form


echo "<br />Top 10 in Area<br />";
foreach($areaArray as $count => $area)
echo "Area [$count] : $area <br>";

echo "<br />Top 10 in Population<br />";


foreach($populationArray as $count => $population)
echo "Population [$count] : $population <br>";

$buttonSubmit = $_POST[‘hantar’];
switch ($buttonSubmit)
{
case "Different":
$ResultArray = array_diff($areaArray, $arrayPopulation);
$ResultArray = array_value($ResultArray);
echo “<br /> The following of the most populous countries are not
also the largest in area: <br />”;
asort($ResultArray);

Page 2 of 3
foreach($ResultArray as $count => $result)
echo "Country [$count] : $result <br>";
break;

case "Intersection":
$ResultArray = array_intersec($areaArray, $arrayPopulation);
$ResultArray = array_value($ResultArray);
echo “<br /> The following of the most populous countries are
also among the largest in area: <br />”;
asort($ResultArray);
foreach($ResultArray as $count => $result)
echo "Country [$count] : $result <br>";
break;
}

?>
</body>
</html>

Step 3 : Run your compareForm.php and input the following value:

Area : Russia, China, Canada, United States, Brazil, Australia, India, Argentina, Kazakhstan, Algeria
Population : China, India, United States, Indonesia, Brazil, Pakistan, Bangladesh, Rusia, igeria, Japan

Step 4: Click on Different Button. What are output? Explain your answer. Submit your answer on kelip
assignment 2 Step 4.

Step 5: Using the same input, click on Intersection Button. What are output? Explain your answer.
Submit your answer on kelip assignment 2 Step 5.

Assignment 3: Simple Application

Write a program to input daily temperature for 30 days and one button (sort and Count). After user click
sorting and count buttons, it will invoke the tempProcess.php to display daily temperature in sorted
value and count the number of hot days, pleasant days as well as cold days in a month according to the
table below:

Temperature Type of days


85 and above hot
55 to 84 pleasant
Below 54 cool

Page 3 of 3

You might also like