📝 PHP Practical Exam – Arrays
📌 Subject: PHP Programming
⏱ Time: 2 Hours
🎯 Total Marks: 50
✅ Section A: Basic Programs (10 Marks)
🔹 Q1 (5 Marks)
Create an indexed array of 5 numbers.
- Print all elements
- Print the sum of all numbers
🔹 Q2 (5 Marks)
Create an array of 5 fruits and display them using a for loop.
✅ Section B: Associative Arrays (10 Marks)
🔹 Q3 (5 Marks)
Create an associative array for a student:
(name, age, course)
- Print all details using
foreach
🔹 Q4 (5 Marks)
Create an associative array of subjects and marks.
- Find and display the highest marks
✅ Section C: Multidimensional Arrays (10 Marks)
🔹 Q5 (5 Marks)
Create a multidimensional array of 3 students:
(name, age, course)
- Print all student names
🔹 Q6 (5 Marks)
Display student data in this format:
Aditya - BCA
Rahul - BSc
Neha - MCA
✅ Section D: Array Functions (10 Marks)
🔹 Q7 (5 Marks)
Create an array of numbers:
- Sort in ascending order
- Sort in descending order
🔹 Q8 (5 Marks)
Merge two arrays and display the result.
✅ Section E: Logic-Based Questions (10 Marks)
🔹 Q9 (5 Marks)
Create an array of numbers.
- Use
array_filter()to display only even numbers
🔹 Q10 (5 Marks)
Use array_map() to create a new array with square of numbers.
🚀 Bonus Question (Optional – 5 Marks)
🔹 Q11
Create a simple search system:
- Store 5 names in an array
- Take user input
- Check if name exists or not
🎯 Marking Scheme
| Section | Marks |
|---|---|
| A | 10 |
| B | 10 |
| C | 10 |
| D | 10 |
| E | 10 |
| Bonus | +5 |
💡 Instructions for Students
- Write clean and readable code
- Use proper syntax
- Output must be displayed clearly
- Use comments where necessary
Full Answer Key (Solutions) – PHP Arrays Practical Exam
📝 ✅ Section A: Basic Programs
🔹 Q1 Solution
$numbers = [10, 20, 30, 40, 50];// Print elements
foreach ($numbers as $num) {
echo $num . " ";
}// Sum
$sum = array_sum($numbers);
echo "<br>Sum: " . $sum;
🔹 Q2 Solution
$fruits = ["Apple", "Banana", "Mango", "Orange", "Grapes"];for ($i = 0; $i < count($fruits); $i++) {
echo $fruits[$i] . "<br>";
}
📝 ✅ Section B: Associative Arrays
🔹 Q3 Solution
$student = [
"name" => "Aditya",
"age" => 22,
"course" => "BCA"
];foreach ($student as $key => $value) {
echo "$key: $value <br>";
}
🔹 Q4 Solution
$marks = [
"Math" => 80,
"Science" => 90,
"English" => 75
];$max = max($marks);
echo "Highest Marks: " . $max;
📝 ✅ Section C: Multidimensional Arrays
🔹 Q5 Solution
$students = [
["Aditya", 22, "BCA"],
["Rahul", 21, "BSc"],
["Neha", 23, "MCA"]
];foreach ($students as $student) {
echo $student[0] . "<br>";
}
🔹 Q6 Solution
$students = [
["Aditya", 22, "BCA"],
["Rahul", 21, "BSc"],
["Neha", 23, "MCA"]
];foreach ($students as $student) {
echo $student[0] . " - " . $student[2] . "<br>";
}
📝 ✅ Section D: Array Functions
🔹 Q7 Solution
$numbers = [5, 2, 9, 1, 7];// Ascending
sort($numbers);
echo "Ascending: ";
print_r($numbers);// Descending
rsort($numbers);
echo "<br>Descending: ";
print_r($numbers);
🔹 Q8 Solution
$a = ["Red", "Blue"];
$b = ["Green", "Yellow"];$result = array_merge($a, $b);
print_r($result);
📝 ✅ Section E: Logic-Based Questions
🔹 Q9 Solution
$numbers = [1, 2, 3, 4, 5, 6];$even = array_filter($numbers, function($n) {
return $n % 2 == 0;
});print_r($even);
🔹 Q10 Solution
$numbers = [1, 2, 3, 4];$square = array_map(function($n) {
return $n * $n;
}, $numbers);print_r($square);
🚀 ✅ Bonus Solution
🔹 Q11 Solution (Search System)
$names = ["Aditya", "Rahul", "Neha", "Amit", "Priya"];$search = "Rahul"; // You can take user input hereif (in_array($search, $names)) {
echo "Name found!";
} else {
echo "Name not found!";
}
🎯 Extra Improvement (Advanced Version)
👉 With user input:
$names = ["Aditya", "Rahul", "Neha", "Amit", "Priya"];$search = $_GET['name'];if (in_array($search, $names)) {
echo "Name found!";
} else {
echo "Name not found!";
}






