<?php
include('includes/header.php');
error_reporting(E_ALL); // Show all errors
echo("<h1>Evan's PHP Array Assignment</h1>");
?>
<!-- HTML Form to collect user input -->
<h2>Enter names to populate an array</h2>
<form method="post">
<label>Enter names (comma-separated):</label><br>
<input type="text" name="names" placeholder="e.g., Alice, Bob, Charlie"><br><br>
<input type="submit" value="Submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['names'])) {
echo('<h2>Original Input and Array Processing</h2>');
$input = $_POST['names'];
// Convert comma-separated input to array
$names = array_map('trim', explode(',', $input));
echo('<h3>Original Array</h3>');
echo('<pre>');
print_r($names);
echo('</pre>');
// Count elements
$count = count($names);
echo("<p><strong>Number of names:</strong> $count</p>");
// Add additional names
array_push($names, "Zara", "Evan");
echo('<h3>Array After Adding More Names</h3>');
echo('<pre>');
print_r($names);
echo('</pre>');
// Sort the array
sort($names);
echo('<h3>Sorted Array (A-Z)</h3>');
echo('<pre>');
print_r($names);
echo('</pre>');
// Reverse the array
$reversed = array_reverse($names);
echo('<h3>Reversed Array</h3>');
echo('<pre>');
print_r($reversed);
echo('</pre>');
// Unique values
$unique = array_unique($names);
echo('<h3>Unique Values Only</h3>');
echo('<pre>');
print_r($unique);
echo('</pre>');
// Search for a specific name
$searchName = "Alice";
$foundIndex = array_search($searchName, $names);
echo("<h3>Searching for '$searchName'</h3>");
if ($foundIndex !== false) {
echo("<p>'$searchName' found at index: $foundIndex</p>");
} else {
echo("<p>'$searchName' not found in the array.</p>");
}
// Combine into associative array (ID => Name)
$ids = range(1, count($names));
$assocArray = array_combine($ids, $names);
echo('<h3>Associative Array (ID => Name)</h3>');
echo('<pre>');
print_r($assocArray);
echo('</pre>');
}
include('includes/footer.php');
?>