Forum Moderators: coopster
$array = (12,13,26,45,53,89);
$range = range(12,30);
$number = max_in_range($range,$array)
// output $number = 26
I am just not quite sure what my max_in_range would look like? my head just in earth at the moment, can anyone poke me in the right direction please?
Many Thanks,
Alan
Ok, so now i have a methods, but I wonder if maybe there is a better way.
//holder for our category
$our_cat;
// this is our array of id's
$ids = explode(',',$_product->_data['category_ids']);
// our range.
$range = range(54,154);
// sort array to send highest number to the end.
sort($ids);
// loop through, and set our id to $our_cat
foreach($ids as $i){
if(in_array($i,$range)){ $our_cat = $i; }
}
with the above code, our_cat will equal the highest value in my range, but, is there a better way still?
Many Thanks,
Alan.
$number = max_in_range($range, $array);
function max_in_range($ran, $arr) {
$value_count = count($arr);
$min_range = min($ran);
$max_range = max($ran);
for($i = 0; $i < $value_count; $i++) {
if(($arr[$i] >= $min_range) || ($arr[$i] <= $max_range)) {
$range_array[$i] = $arr[$i];
}
}
$output = max($range_array);
return $output;
} That should do the job.
but, is there a better way still?
$array = array(12,13,26,45,53,89);
$range = range(12,30);
$number = max(array_intersect($range,$array));
print '<pre>';
print_r($array);
print_r($range);
print $number;
print '</pre>';
exit;