What improvements can be made to the code snippet to ensure that button actions are correctly associated with the corresponding data in the array?

The issue with the current code snippet is that the button actions are not correctly associated with the corresponding data in the array. To solve this issue, we can pass the index of the data item as a parameter in the button's onclick function. This way, we can access the correct data item when the button is clicked and perform the desired action.

<?php
$data = array("Item 1", "Item 2", "Item 3");

foreach($data as $index => $item) {
    echo "<div>$item <button onclick='handleClick($index)'>Click</button></div>";
}

function handleClick($index) {
    // Access the correct data item using the index
    global $data;
    echo "You clicked on: " . $data[$index];
}
?>