Task-1 (1)
Task-1 (1)
1. Write a PHP script that inserts a new item in an array in any position.
Expected Output :
Original array :
12345
After inserting '$' the array is :
123$45
4. Write a PHP script which displays all the numbers between 200 and 250 that
are divisible by 4.
Note : Do not use any PHP control statement.
Expected Output : 200,204,208,212,216,220,224,228,232,236,240,244,248
You are managing an online store, and you need to calculate the total value of items in stock.
Each product has a price, and you have a list of their prices in an array.
Example input:
$prices = [500, 1200, 350, 800, 2000];
Expected output:
Total value: 4850
A weather monitoring system stores the daily temperatures of a week in an array. You need
to find the highest and lowest temperatures recorded.
Example input:
php
Expected output:
php
Your website has a subscription list, but some users have subscribed multiple times. You
need to remove duplicate email addresses to ensure each user receives only one newsletter.
Example input:
php
Expected output:
php
A restaurant receives orders from two different online platforms. Sometimes, the same items
appear in both order lists. You need to merge both lists and remove duplicate items.
Write a function mergeOrders($orders1, $orders2) that merges two order lists while
ensuring each item appears only once.
Example input:
php
Expected output:
php
A retail store wants to analyze sales data to determine the number of times each product was
sold.
Example input:
php
Expected output:
php
Example input:
php
$participants = [
"John" => "Male",
"Sarah" => "Female",
"Mike" => "Male",
"Emma" => "Female"
];
Expected output:
php
A messaging app displays the latest messages first. However, the data is stored in
chronological order.
Example input:
php
Expected output:
php
A web developer needs to check whether a database table structure is indexed (numeric keys)
or associative (column names as keys).
php
Expected output:
php
A school wants to find the second highest exam score from a list of student marks.
Example input:
php
Expected output:
php
92
A movie recommendation system filters out short movie titles and only recommends movies
with titles longer than a certain number of characters.
Example input:
php
Expected output:
2.function findTemperatureRange($temps) {
return ["max" => max($temps), "min" => min($temps)];
}
3. function removeDuplicateSubscribers($emails) {
return array_unique($emails);
}
6. function separateParticipants($participants) {
$result = ["Male" => [], "Female" => []];
foreach ($participants as $name => $gender) {
$result[$gender][] = $name;
}
return $result;
}
$participants = [
"John" => "Male",
"Sarah" => "Female",
"Mike" => "Male",
"Emma" => "Female"
];
print_r(separateParticipants($participants));
7. function reverseMessages($messages) {
$reversed = [];
for ($i = count($messages) - 1; $i >= 0; $i--) {
$reversed[] = $messages[$i];
}
return $reversed;
}
8. function isAssociativeArray($arr) {
return array_keys($arr) !== range(0, count($arr) - 1);
}
9. function secondHighestScore($scores) {
$max = max($scores);
$secondMax = PHP_INT_MIN;
foreach ($scores as $score) {
if ($score < $max && $score > $secondMax) {
$secondMax = $score;
}
}
return $secondMax;
}