Reputation: 672
I have this function and i want to get the highest number value. $array is out-putting 20, 50, 40. I want to print 50 only not the other 2. Tried everything nothing is working.
foreach ($products as $product) {
$originalPriceCat = $product->getPrice();
$finalPriceCat = $product->getFinalPrice();
if ($originalPriceCat > $finalPriceCat) {
$CalculatedPrice = ($originalPriceCat - $finalPriceCat) * 100 /$originalPriceCat;
$array = array($CalculatedPrice);
echo round(max($array));
}
}
Upvotes: 1
Views: 109
Reputation: 2187
Take a look at this php function: max
max ( array $values ) : mixed
max ( mixed $value1 [, mixed $... ] ) : mixed
If the first and only parameter is an array, max() returns the highest value in that array. If at least two parameters are provided, max() returns the biggest of these values.
max() returns the parameter value considered "highest" according to standard comparisons.
If multiple values of different types evaluate as equal (e.g. 0 and 'abc') the first provided to the function will be returned.
If an empty array is passed, then FALSE
will be returned and an E_WARNING
error will be emitted.
Upvotes: 2
Reputation: 2769
The following code will surely solve your problem
$CalculatedPrice = array();
foreach ($products as $product) {
$originalPriceCat = $product->getPrice();
$finalPriceCat = $product->getFinalPrice();
if ($originalPriceCat > $finalPriceCat) {
$CalculatedPrice[] = ($originalPriceCat - $finalPriceCat) * 100 / $originalPriceCat;
}
}
echo round(max($CalculatedPrice));
Upvotes: 3