How can PHP be used to dynamically update array elements based on user input in a form?

To dynamically update array elements based on user input in a form using PHP, you can first retrieve the user input using $_POST superglobal array. Then, you can use this input to update the corresponding array elements based on the user's actions. This can be achieved by assigning the user input to specific array keys or by using loops to iterate through the array and update elements accordingly.

<?php
// Sample array
$fruits = array("apple", "banana", "cherry");

// Check if form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST") {
    // Retrieve user input
    $index = $_POST['index'];
    $newFruit = $_POST['newFruit'];

    // Update array element based on user input
    $fruits[$index] = $newFruit;
}

// Display form
echo "<form method='post'>";
echo "<input type='text' name='index' placeholder='Enter index'>";
echo "<input type='text' name='newFruit' placeholder='Enter new fruit'>";
echo "<input type='submit' value='Update'>";
echo "</form>";

// Display updated array
print_r($fruits);
?>