<?php
include('includes/header.php');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Array Manipulation in PHP</title>
</head>
<body>
<h1>Array Input Form</h1>
<form method="post">
<label for="values">Enter values (comma-separated):</label>
<input type="text" name="values" id="values" required>
<button type="submit">Submit</button>
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Step 2: Get the values from the form
$input_values = $_POST["values"];
// Step 3: Convert the input string to an array
$array = explode(",", $input_values);
// Step 4: Display the original array
echo "<h2>Original Array:</h2>";
echo "<pre>";
print_r($array);
echo "</pre>";
// Step 5: Perform Array Manipulations
// 1. Sort the array
sort($array);
echo "<h2>Sorted Array:</h2>";
echo "<pre>";
print_r($array);
echo "</pre>";
// 2. Reverse the array
$reversed_array = array_reverse($array);
echo "<h2>Reversed Array:</h2>";
echo "<pre>";
print_r($reversed_array);
echo "</pre>";
// 3. Add a new element to the array
array_push($array, "New Element");
echo "<h2>Array after adding an element:</h2>";
echo "<pre>";
print_r($array);
echo "</pre>";
// 4. Remove the last element
array_pop($array);
echo "<h2>Array after removing the last element:</h2>";
echo "<pre>";
print_r($array);
echo "</pre>";
// 5. Check if an element exists in the array
$element = "5"; // You can change this to any number or string you want to check
if (in_array($element, $array)) {
echo "<h2>Element '$element' exists in the array!</h2>";
} else {
echo "<h2>Element '$element' does NOT exist in the array.</h2>";
}
// 6. Combine the array into a string
$array_string = implode(", ", $array);
echo "<h2>Array as a string:</h2>";
echo "<p>$array_string</p>";
// 7. Get the length of the array
$array_length = count($array);
echo "<h2>Array Length:</h2>";
echo "<p>$array_length</p>";
}
?>
<?php
echo("<br><br>");
include('includes/footer.php');
?>
</body>
</html>