我试图从内部算法访问GCD,但它不让我,我不知道为什么。我在这里做错了什么?
public function gcd($x,$y)
{
do {
$rest=$x%$y;
$x=$y;
$y=$rest;
} while($rest!==0);
return $x;
}
public function algorithm()
{
$alpha = array(
'c' => str_split('bcdfghjklmnpqrstvwxz'),
'v' => str_split('aeiouy')
);
$i=$k=0;
foreach ($this->output as $item) {
$cnt = 0;
$this->digits[$i] = array();
foreach ($item as $part) {
$this->digits[$i][$cnt] = array();
$new = array();
foreach ($part as $str) {
$v = count(array_intersect(str_split($str), $alpha['v']));
$c = count(array_intersect(str_split($str), $alpha['c']));
$t = strlen(str_replace(' ', '', $str));
$new = ($cnt == 0)
? array('v' => $v, 'c' => $c, 't' => $t, 'm' => ($t%2) ? $v * 1.5 : $c)
: array('v' => $v, 'c' => $c, 't' => $t);
$this->digits[$i][$cnt][] = $new;
}
$cnt++;
}
$i++;
}
$h=$a=0;
foreach($this->digits as &$etc) {
foreach($etc[0] as &$r){
foreach($etc[1] as $k) {
foreach($k as $x=>$y) {
$tmp[$h] = (gcd($y,$r['t']) != 1) ? ++$a:'';
}
$tmp[$h] = $r['m']*$a*1.5;
$h++;
$a=0;
}$h=0;
$r['f'] = $tmp;
$tmp='';
}
}
foreach($this->digits as &$u){unset($u[1]);}
}
答案 0 :(得分:1)
通过使用public
标识符来判断,我猜你的两个函数都属于一个类。
要引用同一对象上的方法,请使用$this->methodname()
来自您的代码:
$tmp[$h] = (gcd($y,$r['t']) != 1) ? ++$a:'';
应该是:
$tmp[$h] = ($this->gcd($y,$r['t']) != 1) ? ++$a:'';
答案 1 :(得分:0)
...
$this->gcd()?
但实际上,由于gcd
不使用任何成员变量,因此它应该是一个自由函数。
答案 2 :(得分:0)
你错过了$ this-> gcd里面的算法 只是试图直接访问它 :)