What is the correct way to store an array in a session in PHP?

When storing an array in a session in PHP, you need to first start the session using session_start(). Then, you can simply assign the array to a session variable using $_SESSION['key'] = $array. This will serialize the array and store it in the session. To access the array later, you can simply retrieve it from the session using $_SESSION['key'].

<?php
session_start();

$array = [1, 2, 3, 4, 5];
$_SESSION['myArray'] = $array;

// To access the array later
$retrievedArray = $_SESSION['myArray'];
print_r($retrievedArray);
?>