I had a similar need for an application I worked on. Here's what I used; if anybody has any better solutions, I'd sure like to know:
function GetGCD($vars) {
// Ascertain that $vars is an array:
if(is_array($vars)) {
// Count the array elements:
$n = count($vars);
// If there's just one array element, then it must be the GCD. Return:
if($n == 1) {
return $vars[0];
}
// Loop through the array elements:
for($i = 0; $i < $n; $i++) {
// Set up the current comparison values. If there's not a 'temporary'
// GCD yet, we're on the first element:
if(!$a) {
$a = $vars[$i];
}
$b = $vars[$i + 1];
while($b != 0) {
$r = $a % $b;
$a = $b;
$b = $r;
}
$a = abs($a);
}
return $a;
}
}
echo 'GCD: ' . GetGCD(array(52,24,36,48)) . '<br />';
Now I'll see if I can dig up my GetLCM() function outta the archives; if I recall correctly, it used the GetGCD() function...
Have fun...