0% found this document useful (0 votes)
18 views10 pages

Task-1 (1)

The document outlines various PHP programming tasks related to arrays, strings, and functions. It includes scripts for manipulating arrays, sorting, calculating averages, and handling string operations, as well as functions for inventory management, temperature analysis, and participant separation. Each task is accompanied by expected outputs and example inputs to guide implementation.

Uploaded by

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

Task-1 (1)

The document outlines various PHP programming tasks related to arrays, strings, and functions. It includes scripts for manipulating arrays, sorting, calculating averages, and handling string operations, as well as functions for inventory management, temperature analysis, and participant separation. Each task is accompanied by expected outputs and example inputs to guide implementation.

Uploaded by

varnikarathi24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

TASK- ARRAYS

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

2. Write a PHP script to sort the following associative array :


array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40") in
a) ascending order sort by value
b) ascending order sort by Key
c) descending order sorting by Value
d) descending order sorting by Key

3. Write a PHP script to calculate and display average temperature, five


lowest and highest temperatures.
Recorded temperatures : 78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76,
73, 68, 62, 73, 72, 65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 73
Expected Output :
Average Temperature is : 69.76
List of seven lowest temperatures : 60, 62, 63, 63, 64,
List of seven highest temperatures : 76, 78, 79, 81, 85,

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

5. Write a PHP program to remove duplicate values from an array which


contains only strings or only integers.
TASK – STRING

1. Write a PHP script to get the last three characters of a string.


Sample String : '[email protected]'
Expected Output : 'com'

2. Write a PHP script to print letters from 'a' to 'z'.


Expected Result : abcdefghijklmnopqrstuvwxyz

3. Write a PHP script to split the following string.


Sample string : '082307'
Expected Output : 08:23:07
TASK – FUNCTIONS

1. Write a function to calculate the factorial of a number (a non-negative


integer). The function accepts the number as an argument.
2. Write a function to check whether a number is prime or not.
3. Write a function to reverse a string.
4. Write a function to sort an array.
5. Write a PHP function that checks whether a passed string is a
palindrome or not?
1. Store Inventory Total Value

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.

Write a PHP function calculateTotalValue($prices) that takes an array of item prices


and returns the total value of inventory.

Example input:
$prices = [500, 1200, 350, 800, 2000];
Expected output:
Total value: 4850

2. Identify Highest and Lowest Temperature of the Week

A weather monitoring system stores the daily temperatures of a week in an array. You need
to find the highest and lowest temperatures recorded.

Write a function findTemperatureRange($temps) that returns an associative array with max


and min temperatures.

Example input:

php

$temps = [25, 30, 22, 35, 28, 31, 27];

Expected output:

php

["max" => 35, "min" => 22]

3. Remove Duplicate Email Subscribers

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.

Write a function removeDuplicateSubscribers($emails) that takes an array of email


addresses and returns a unique list.

Example input:

php

$subscribers = ["[email protected]", "[email protected]", "[email protected]",


"[email protected]"];

Expected output:
php

["[email protected]", "[email protected]", "[email protected]"]

4. Merging Customer Orders Without Duplicates

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

$orders1 = ["Burger", "Pizza", "Pasta"];


$orders2 = ["Pizza", "Salad", "Pasta", "Soup"];

Expected output:

php

["Burger", "Pizza", "Pasta", "Salad", "Soup"]

5. Count How Many Times a Product Was Sold

A retail store wants to analyze sales data to determine the number of times each product was
sold.

Write a function countProductSales($sales, $product) that takes an array of sold items


and a specific product name, returning how many times that product was sold.

Example input:

php

$sales = ["Laptop", "Phone", "Tablet", "Laptop", "Phone", "Laptop",


"Tablet"];
$product = "Laptop";

Expected output:

php

6. Separate Male and Female Participants in an Event


An event registration system collects participant data, including gender. You need to separate
male and female participants into two different lists.

Write a function separateParticipants($participants) that takes an associative array


with names and gender and returns separate lists.

Example input:

php

$participants = [
"John" => "Male",
"Sarah" => "Female",
"Mike" => "Male",
"Emma" => "Female"
];

Expected output:

php

["Male" => ["John", "Mike"], "Female" => ["Sarah", "Emma"]]

7. Reverse a List of Recent Chat Messages

A messaging app displays the latest messages first. However, the data is stored in
chronological order.

Write a function reverseMessages($messages) that reverses the order of an array of chat


messages without using array_reverse().

Example input:

php

$messages = ["Hello", "How are you?", "I'm fine", "Great!"];

Expected output:

php

["Great!", "I'm fine", "How are you?", "Hello"]

8. Check if a Database Table Structure is Associative or Indexed

A web developer needs to check whether a database table structure is indexed (numeric keys)
or associative (column names as keys).

Write a function isAssociativeArray($arr) that checks whether an array is indexed or


associative.
Example input:

php

$data1 = ["id" => 1, "name" => "Alice", "email" => "[email protected]"];


$data2 = ["Apple", "Banana", "Cherry"];

Expected output:

php

$data1 is an associative array.


$data2 is an indexed array.

9. Find the Second Highest Score in an Exam

A school wants to find the second highest exam score from a list of student marks.

Write a function secondHighestScore($scores) that returns the second highest score


without using sorting functions.

Example input:

php

$scores = [85, 92, 78, 96, 88, 89, 92];

Expected output:

php

92

10. Find Long Movie Titles for a Movie Recommendation System

A movie recommendation system filters out short movie titles and only recommends movies
with titles longer than a certain number of characters.

Write a function filterLongMovies($movies, $length) that takes an array of movie titles


and a minimum character length, returning only movies with longer titles.

Example input:

php

$movies = ["Avatar", "The Dark Knight", "Inception", "Up", "Interstellar"];


$length = 10;

Expected output:

Php ["The Dark Knight", "Interstellar"]


1.function calculateTotalValue($prices) {
return array_sum($prices);
}

$prices = [500, 1200, 350, 800, 2000];


echo "Total value: " . calculateTotalValue($prices);

2.function findTemperatureRange($temps) {
return ["max" => max($temps), "min" => min($temps)];
}

$temps = [25, 30, 22, 35, 28, 31, 27];


print_r(findTemperatureRange($temps));

3. function removeDuplicateSubscribers($emails) {
return array_unique($emails);
}

$subscribers = ["[email protected]", "[email protected]", "[email protected]",


"[email protected]"];
print_r(removeDuplicateSubscribers($subscribers));

4. function mergeOrders($orders1, $orders2) {


return array_values(array_unique(array_merge($orders1, $orders2)));
}

$orders1 = ["Burger", "Pizza", "Pasta"];


$orders2 = ["Pizza", "Salad", "Pasta", "Soup"];
print_r(mergeOrders($orders1, $orders2));

5. function countProductSales($sales, $product) {


return array_count_values($sales)[$product] ?? 0;
}

$sales = ["Laptop", "Phone", "Tablet", "Laptop", "Phone", "Laptop", "Tablet"];


$product = "Laptop";
echo countProductSales($sales, $product);

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;
}

$messages = ["Hello", "How are you?", "I'm fine", "Great!"];


print_r(reverseMessages($messages));

8. function isAssociativeArray($arr) {
return array_keys($arr) !== range(0, count($arr) - 1);
}

$data1 = ["id" => 1, "name" => "Alice", "email" => "[email protected]"];


$data2 = ["Apple", "Banana", "Cherry"];

echo isAssociativeArray($data1) ? "Associative array\n" : "Indexed array\n";


echo isAssociativeArray($data2) ? "Associative array\n" : "Indexed array\n";

9. function secondHighestScore($scores) {
$max = max($scores);
$secondMax = PHP_INT_MIN;
foreach ($scores as $score) {
if ($score < $max && $score > $secondMax) {
$secondMax = $score;
}
}
return $secondMax;
}

$scores = [85, 92, 78, 96, 88, 89, 92];


echo secondHighestScore($scores);

10. function filterLongMovies($movies, $length) {


return array_filter($movies, function($movie) use ($length) {
return strlen($movie) > $length;
});
}

$movies = ["Avatar", "The Dark Knight", "Inception", "Up", "Interstellar"];


$length = 10;
print_r(filterLongMovies($movies, $length));

You might also like