PHP PR 4,6 (22203C0007)
PHP PR 4,6 (22203C0007)
Code
<?php
$arr1 = array("Vashi", "Nerul", "Panvel", "Lower Parel");
echo "Using for loop \n";
for($i = 0; $i < count($arr1); $i++){
echo "City is : $arr1[$i] \n";
}
$allKeys = array_keys($arr3);
for($j = 0; $j < count($arr3); $j++){
$key = $allKeys[$j];
$value = $arr3[$key];
echo "$key : $value \n";
}
Output
Q2 Implement all array functions in PHP
Code
<?php
$nums = [5, 15, 10, 20, 25];
echo "Original Array: ";
print_r($nums);
$letters = ['a', 'b', 'c', 'd', 'e'];
$combined = array_combine($letters, $nums);
echo "\nCombined Array: ";
print_r($combined);
unset($nums[2]);
echo "\nAfter Unset (Removed index 2): ";
print_r($nums);
sort($nums);
echo "\nSorted (Ascending): ";
print_r($nums);
rsort($nums);
echo "\nSorted (Descending): ";
print_r($nums);
$associative = ["x" => 12, "y" => 8, "z" => 20, "a" => 15];
asort($associative);
echo "\nAssociative Array Sorted by Value (Ascending): ";
print_r($associative);
arsort($associative);
echo "\n>Associative Array Sorted by Value (Descending): ";
print_r($associative);
ksort($associative);
echo "\nAssociative Array Sorted by Key (Ascending): ";
print_r($associative);
krsort($associative);
echo "\nAssociative Array Sorted by Key (Descending): ";
print_r($associative);
array_push($nums, 30);
array_unshift($nums, 3);
echo "\nAfter Push and Unshift: ";
print_r($nums);
array_pop($nums);
array_shift($nums);
echo "\nAfter Pop and Shift: ";
print_r($nums);
$reversed = array_reverse($nums);
echo "\nReversed Array: ";
print_r($reversed);
$sum = array_sum($nums);
echo "\nSum of Elements: $sum";
$nums2 = [10, 20, 40, 50];
$diff = array_diff($nums, $nums2);
echo "\nArray Difference: ";
print_r($diff);
$intersect = array_intersect($nums, $nums2);
echo "\nArray Intersection: ";
print_r($intersect);
$chunked = array_chunk($nums, 2);
echo "\nChunked Array: ";
print_r($chunked);
$merged = array_merge($nums, $nums2);
echo "\nMerged Array: ";
print_r($merged);
$pr = array_product($nums);
echo "\nProduct of Elements: $pr";
$search = array_search(20, $nums);
echo "\nPosition of 20: $search";
$implode = implode(", ", $nums);
echo "\nImploded Array: $implode";
$explode = explode(", ", $implode);
echo "\nExploded Array: ";
print_r($explode);
echo "\nCount of Elements: " . count($nums);
$count = array_count_values($nums);
echo "\nCount of Each Value: ";
print_r($count);
$unique = array_unique($nums);
echo "\nUnique Values: ";
print_r($unique);
?>
Output
Q3 Write a php script to manage and analyze daily orders of restaurant the
script should have following functionality's
1.Add new order
2.Remove an order
3.Get total number of orders placed
4.Display the list of orders placed
5.Identify most ordered dish
Code
<?php
$orders = [];
function getTotalOrders(&$orders) {
return count($orders);
}
function getUniqueDishes(&$orders) {
return array_unique($orders);
}
function getMostOrderedDish(&$orders) {
$dishCount = array_count_values($orders);
if (empty($dishCount)) {
echo "No dishes ordered yet.\n";
return;
}
$maxOrders = max($dishCount);
placeOrder($orders, "Idli");
placeOrder($orders, "Idli");
placeOrder($orders, "Tea");
removeOrder($orders, "Burger");
?>
Output
Code
<?php
function add($a1,$a2){
return $a1 + $a2;
}
$name = 'add';
echo "\nFunction call\n";
echo "Result " . $name(5,10);
?>
Output
Code
<?php
$addition = function($a1,$a2){
return $a1 + $a2;
};
echo "\nFunction call\n";
echo "Result " . $addition(34,10);
?>
Output