What are the potential pitfalls of using array_intersect or array_diff functions in PHP?

Using array_intersect or array_diff functions in PHP can lead to unexpected results if the arrays being compared are multidimensional or contain complex data types. To avoid this issue, you can implement a custom comparison function that properly handles the comparison logic for complex data types.

// Custom function to compare complex data types
function custom_array_intersect($array1, $array2) {
    $result = array();
    foreach ($array1 as $value) {
        if (in_array($value, $array2)) {
            $result[] = $value;
        }
    }
    return $result;
}

// Usage example
$array1 = [1, 'apple', ['color' => 'red']];
$array2 = ['apple', ['color' => 'red'], 3];

$result = custom_array_intersect($array1, $array2);
print_r($result);