| Author | Message |
John Richardson
15 posts
Location: United Kingdom Occupation: Age: |
#14 02-02-2010 10:13 GMT |
|
Php max function is a quick and dirty way to extract the highest value but as I found out recently you can come unstuck. I needed some thing that could evaluate strings such as 3aA, 2b3, 3a1 the following bubblesort routine to arrange in order and array_pop to get the last value from the sorted array did the trick.
function maxjr($sort_array,$reverse){
for ($i = 0; $i < sizeof($sort_array); $i++){
for ($j = $i + 1; $j < sizeof($sort_array); $j++){
if($reverse){
if ($sort_array[$i] < $sort_array[$j]){
$tmp = $sort_array[$i];
$sort_array[$i] = $sort_array[$j];
$sort_array[$j] = $tmp;
}
} else {
if ($sort_array[$i] > $sort_array[$j]){
$tmp = $sort_array[$i];
$sort_array[$i] = $sort_array[$j];
$sort_array[$j] = $tmp;
}
}
}
}
$max_val = array_pop($sort_array);
return $max_val;
}
Usage: $maxvalue = maxjr($input_array); |
|




