What are the advantages and disadvantages of using an array to store maximum values in PHP loops?

When using a loop in PHP to find the maximum value of an array, one common approach is to store the maximum value in a separate variable. However, if you need to keep track of multiple maximum values in different iterations of the loop, using an array to store these values can be more efficient. This allows you to easily access and compare the maximum values later in your code. However, using an array may consume more memory compared to just storing a single maximum value variable.

// Initialize an empty array to store maximum values
$maxValues = [];

// Loop through an array to find maximum values
foreach ($array as $value) {
    // Compare current value with existing maximum values
    foreach ($maxValues as $maxValue) {
        if ($value > $maxValue) {
            $maxValues[] = $value;
            break;
        }
    }
}

// Access maximum values stored in the array
foreach ($maxValues as $maxValue) {
    echo $maxValue . "\n";
}