What potential pitfalls can arise when using array_unique() in PHP?

When using array_unique() in PHP, one potential pitfall is that it only removes duplicate values based on their string representation. This means that it may not work as expected with arrays containing nested arrays or objects. To ensure that array_unique() removes duplicates correctly, you can serialize the values before applying the function.

// Example array with nested arrays
$array = [
    [1, 2],
    [1, 2],
    [3, 4]
];

// Serialize each value before using array_unique
$serializedArray = array_map('serialize', $array);
$uniqueArray = array_unique($serializedArray);

// Unserialize the values to get the unique array
$finalArray = array_map('unserialize', $uniqueArray);

print_r($finalArray);