22203a0063 Prac 4,6
22203a0063 Prac 4,6
4 Write a PHP program for creating and 1. Script to traverse indexed array
manipulating a)Indexed array ,associative array and multidimensional
b)associative array c)Multidimensional array using for and foreach loop.
array 2. Script to implement array inbuilt
function
3.Write php script to manage and
analyze daily order restaurent should
develop following requirements:
a. add new orders
b. delete and orders
6 Write a PHP program for creating and c. count total no of orders
manipulating a)Indexed array d. identify most ordered dish for the day
b)associative array c)Multidimensional e. display all the dishes ordered without
duplication
array
4.Write a script to implement concept
of variable function
5.Write a script to implement concept
of anonymous function
XI. Exercise
<?php
$subjects=array("PHP","MAD","MANAGEMENT","PYTHON","EDE");
for ($x=0;$x<5;$x++)
{
print ($subjects[$x]."\n");
}
?>
.
Develop a program for indexed array using for each loop
<?php
$subjects=array("PHP","MAD","MANAGEMENT","PYTHON","EDE");
foreach ($subjects as $i)
{
print ($i."\n");
}
?
Output:
<?php
$data=array("PHP"=>20,"MAD"=>30,"MANAGEMENT"=>20,"PYTHON"=>45,"EDE"=>20);
foreach ($data as $sub=>$m)
{
print ("<br>subject:".$sub."<br>marks:".$m);
print "<br>";
}
?>
Output:
for ($i=0;$i<count($data);$i++)
{
$marks=$data[$subjects[$i]];
print ("<br>subject:".$subjects[$i]);
print ("<br>marks:".$marks);
print"<br>";
}
?>
Output:
Develop a program for multidimensional array using for loop
<?php
$students = array(
array("RollNo" => 47, "Name" => "Shrushti", "Marks" => 85),
array("RollNo" => 49, "Name" => "Sanjana", "Marks" => 90),
array("RollNo" => 36, "Name" => "Shalaka", "Marks" => 88)
);
<?php
$students = array(
47 => array("name" => "Shrushti", "age" => 20, "grade" => "A"),
49 => array("name" => "Sanjana", "age" => 21, "grade" => "B"),
36 => array("name" => "Shalaka", "age" => 22, "grade" => "A")
);
?>
Output:
3. Write php script to manage and analyze daily order restaurent should develop following
requirements:
1. add new orders
2. delete and orders
3. count total no of orders
4. identify most ordered dish for the day
5. display all the dishes ordered without duplication*/
<?php
$orders=[];
//add dish
function addNewOrder(&$orders, $dish){
$orders[]=$dish;
echo ("\nOrder Placed: $dish");
}
//delete dish
function deleteOrder(&$orders, $index){
if (empty($orders)){
echo "\nNo such item found";
}
else{
echo ("\nOrder Placed: $orders[$index]");
unset($orders[$index]);
}
}
//count dish
function countAllDishes(&$orders){
return count($orders);
}
//unique dish
function dishUnique(&$orders){
return (array_unique($orders));
}
//most ordered dish
function mostOrdered(&$orders){
array_count_values($orders);
arsort($orders);
$most=$orders[0];
return $most;
}
addNewOrder($orders, "chakli");
addNewOrder($orders, "karanji");
addNewOrder($orders, "shev");
addNewOrder($orders, "besan ladoo");
addNewOrder($orders, "tes");
addNewOrder($orders, "tea");
deleteOrder($orders, 1);
echo ("\n\nOrder Summary\n");
echo ("\nDishes ordered: ");
print_r ($orders);
echo ("\nNo of dishes placed: ".countAllDishes($orders)."\n");
echo ("\nMost ordered dish: ".mostOrdered($orders)."\n");
print ("\nUnique dishes: ");
print_r(dishUnique($orders));
?>
Output:
4. Write a script implement concept of variable function
<?php
function greet() {
echo "Hello, Welcome to PHP!";
}
$func = "greet";
$func();
?>
Output: