I am trying to get the Min and Max values of an array that is sorted with _price
, _sale_price
, and _regular_price
, but has ids as the first key, and I need to keep the association of the ID value attached to these prices. I am using the code below... but the problem with this is that it returns a key => value pair back to me, and the other problem with this is that I get min("")
returned for the min(_price)
value, but I need it to return the minimum positive number in the group instead.
function GetMinMaxPrices(Array $prices)
{
if (empty($prices)) return array();
// Get highest and lowest for all keys: _regular_price, _sale_price, and _price.
foreach($prices as $varBar => $price_info)
{
foreach($price_info as $type => $value)
{
switch($type)
{
CASE "_price":
$_price[$varBar]['_price'] = array($varBar => $value);
break;
CASE "_sale_price":
$_sale_price[$varBar]['_sale_price'] = array($varBar => $value);
break;
CASE "_regular_price":
$_regular_price[$varBar]['_regular_price'] = array($varBar => $value);
break;
}
}
}
return array(
'_price' => array(
'max' => max(array_map("max", $_price)),
'min' => min(array_map("min", $_price))
),
'_sale_price' => array(
'max' => max(array_map("max", $_sale_price)),
'min' => min(array_map("min", $_sale_price))
),
'_regular_price' => array(
'max' => max(array_map("max", $_regular_price)),
'min' => min(array_map("min", $_regular_price))
)
);
}
$newArray[234290911]['_price'] = '2';
$newArray[234230495]['_price'] = '35';
$newArray[239402343]['_price'] = '';
$newArray[239402343]['_regular_price'] = 65;
$newArray[234290911]['_regular_price'] = 70;
$newArray[234230495]['_regular_price'] = 35;
$newArray[234290911]['_sale_price'] = 1;
$newArray[239402343]['_sale_price'] = 1;
$newArray[234230495]['_sale_price'] = 10;
$newPrices = GetMinMaxPrices($newArray);
var_dump($newPrices);
There may be situations where an empty string is found within some of the prices, in that case, it needs to skip it. If all values for a given group (_price
, _sale_price
, or _regular_price
) are an empty string, then it needs to return an empty string for that group (as min currently does), but only if all values in that group are empty strings. Currently, if any value in the group is an empty string, it returns it as the min
value.
I understand that min
is designed to do this, but how to work around this, while still getting the $varBar (the variation ids) association.
The desired array for the return value of GetMinMaxPrices() function would be this, for each group respectively:
array('_price' => array(
'max_value' => $theMaxPriceValue,
'max_id' => $varBar_max_id,
'min_value' => $theMinPriceValue,
'min_id' => $varBar_min_id
), // etc. etc. );
If all values of these are the same, I don't need it. If the min value = "", then I need an actual min value that is greater than 0. If all values in the array for each group, equals an empty string, then it should than return an empty string, and only then.
Seems I'm very close to this in the function, but just can't get min
to return a value that is not an empty string, for example, the _price
array has an empty string in it, and gives me a min
value of "", but should give me a min
value of 2
instead.