What are the potential pitfalls of using array_unique to remove duplicate values in a PHP session array?

Using array_unique to remove duplicate values in a PHP session array can lead to unexpected behavior if the array contains mixed data types or nested arrays. To avoid this issue, you can manually iterate through the array and use a separate array to store unique values.

// Retrieve the session array
$sessionArray = $_SESSION['myArray'];

// Initialize an empty array to store unique values
$uniqueValues = [];

// Iterate through the session array and add unique values to the new array
foreach ($sessionArray as $value) {
    if (!in_array($value, $uniqueValues)) {
        $uniqueValues[] = $value;
    }
}

// Update the session array with unique values
$_SESSION['myArray'] = $uniqueValues;