How can the foreach loop and in_array() function be used effectively in PHP to compare arrays?

When comparing arrays in PHP, you can use a foreach loop to iterate over one array and then use the in_array() function to check if each element exists in the other array. This combination allows you to efficiently compare the values of two arrays and determine if they are equal or not.

$array1 = [1, 2, 3, 4, 5];
$array2 = [3, 4, 5, 6, 7];

$equal = true;

foreach ($array1 as $value) {
    if (!in_array($value, $array2)) {
        $equal = false;
        break;
    }
}

if ($equal) {
    echo "Arrays are equal";
} else {
    echo "Arrays are not equal";
}