爆炸后制作阵列

时间:2015-03-21 02:52:47

标签: php arrays explode

我想从输入字符串创建一个数组。在此代码之前,我尝试过爆炸,但数组仍然是长度1.我尝试过的每个字符串仍然是数组[0]中的一个。到目前为止,这是我的代码:

public function word()
{
    $kata = array($this->kal->getHasil());
    if (!empty($kata)) {

        $n = count($kata)

        for ($i = 0; $i < $n; $i++) {
            $imin = $i;
            for ($j = $i; $j < $n; $j++) {
                if ($kata[$j] < $kata[$imin]) {
                    $imin = $j;
                }
            }
            $temp = $kata[$i];
            $kata[$i] = $kata[$imin];
            $kata[$imin] = $temp;
        }

        for ($i = 0; $i < $n; $i++) {
            echo "$kata[$i] ";
        }
    }
}

public function tokenize()
{
    $temp = $this->kal->getHasil();
    $token = explode(" ", $temp);
    return $token;
}

$hasil = $pp->tokenize();
for ($i = 0; $i < sizeof($hasil); $i++) {
    $st = new stemming();
    $hasil[$i] = $pp->singkatan($hasil[$i]);
    $hasil[$i] = $st->stem($hasil[$i]);
    $hasil[$i] = $pp->stopWord($hasil[$i]);
    //echo "$hasil[$i] ";

    $hb = new hitungBobot($hasil[$i]);
    $hb->word();
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用全局变量,请参阅代码:

    public function word(){ $kata = array($this->kal->getHasil());

       global $output;

        if(!empty($kata)){

                $ar= count($kata)
                $output += $ar;
            }

     public function tokenize() {
        $temp = $this->kal->getHasil();
        $token = explode(" ",$temp);
        return $token;
    }

    $output = 0;

    $hasil = $pp->tokenize();
                for($i=0; $i<sizeof($hasil); $i++) { 
                    $st = new stemming();
                    $hasil[$i] = $pp->singkatan($hasil[$i]);
                    $hasil[$i] = $st->stem($hasil[$i]);
                    $hasil[$i] = $pp->stopWord($hasil[$i]);
                    //echo "$hasil[$i] ";

                    $hb = new hitungBobot($hasil[$i]);  
                    $hb->word();
                }

echo $output;