0% found this document useful (0 votes)
8 views3 pages

SDL 4

The document contains an HTML code snippet for a PHP application that allows users to search for employee names. It includes an indexed array of employee names, an associative array mapping names to positions, and a multidimensional array with employee details. Upon form submission, the script checks if the entered name exists in each array and displays the corresponding results.

Uploaded by

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

SDL 4

The document contains an HTML code snippet for a PHP application that allows users to search for employee names. It includes an indexed array of employee names, an associative array mapping names to positions, and a multidimensional array with employee details. Upon form submission, the script checks if the entered name exists in each array and displays the corresponding results.

Uploaded by

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

Assignment-4

Code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Array Search</title>
</head>
<body>
<h1>PHP Arrays Demonstration</h1>

<!-- Form to enter the name -->


<form method="post">
<label for="name">Enter Employee Name:</label>
<input type="text" id="name" name="name" required>
<input type="submit" value="Search">
</form>

<?php
// Indexed Array - Employee Names
$employee_names = [
"John", "Alice", "Bob", "Carol", "David", "Eve", "Frank", "Grace", "Hannah", "Ian",
"Jack", "Kathy", "Leo", "Megan", "Nathan", "Olivia", "Paul", "Quincy", "Rita", "Sam"
];

// Associative Array - Employee Name and Position


$employee_positions = [
"John" => "Manager",
"Alice" => "Developer",
"Bob" => "Designer",
"Carol" => "HR",
"David" => "Accountant"
];

// Multidimensional Array - Employee Details


$employees = [
["name" => "John", "age" => 30, "department" => "IT"],
["name" => "Alice", "age" => 25, "department" => "Development"],
["name" => "Bob", "age" => 28, "department" => "Design"],
["name" => "Carol", "age" => 35, "department" => "HR"]
];

// Check if form was submitted


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$search_name = $_POST['name']; // Get the name entered by the user

echo "<h2>Searching for '$search_name' in Indexed Array:</h2>";


if (in_array($search_name, $employee_names)) {
echo "$search_name found in the indexed array.<br>";
} else {
echo "$search_name not found in the indexed array.<br>";
}

echo "<h2>Searching for '$search_name' in Associative Array:</h2>";


if (array_key_exists($search_name, $employee_positions)) {
echo "$search_name found in the associative array as a
$employee_positions[$search_name].<br>";
} else {
echo "$search_name not found in the associative array.<br>";
}

echo "<h2>Searching for '$search_name' in Multidimensional Array:</h2>";


$found = false;
foreach ($employees as $employee) {
if ($employee['name'] == $search_name) {
echo "$search_name found in the multidimensional array. Age: " .
$employee['age'] . ", Department: " . $employee['department'] . ".<br>";
$found = true;
break;
}
}
if (!$found) {
echo "$search_name not found in the multidimensional array.<br>";
}
}
?>
</body>
</html>

Output:

You might also like