What are some best practices for storing and manipulating arrays in PHP sessions to allow for easy deletion of the last entry?

When storing arrays in PHP sessions, it can be tricky to efficiently delete the last entry from the array. One solution is to store the array as a session variable, then manipulate it by removing the last element when needed. To do this, you can use array_pop() function to remove the last element from the array stored in the session.

// Start the session
session_start();

// Check if the array exists in the session
if(isset($_SESSION['my_array'])) {
    // Remove the last element from the array
    array_pop($_SESSION['my_array']);
} else {
    // Initialize the array if it doesn't exist
    $_SESSION['my_array'] = [];
}