簡體   English   中英

遍歷字符串數組

[英]iterate through string array

對於我的代碼: var_dump($POS);

給我:

array (size=9)
  0 => string 'technical' (length=9)
  1 => string 'recruiter' (length=9)
  2 => string 'experience' (length=10)
  3 => string 'work' (length=4)
  4 => string 'department' (length=10)
  5 => string 'modern' (length=6)
  6 => string 'age' (length=3)
  7 => string 'profile' (length=7)
  8 => string 'prakhar' (length=7)

但是當遍歷數組$POS

        foreach($POS as $p)
        {
            echo "POS  :".$p."<br/>";
        }

它僅回顯第一個值,即technical

怎么了?

實際代碼:

    function getTag($post)
    {
            $tagger = new PosTagger('lexicon.txt');
            $temp = $tagger->tag($post);
            $POS = getPOS($temp);
            //var_dump($POS);
            //$POS = implode(", ",$POS);
            $con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');                
            if (mysqli_connect_errno())
            {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
                return;
            }

            $selectTag = mysqli_query($con,"SELECT distinct tag from koove_tags");
            $i = 0;
            $totalPOSs = '';
            $POSs = '';
            $distinctPOSs = '';
            //$prob[$i] = new stdClass();

                    //calculate distinct POS in all POSs for all tags
                    $selectPOS3 = mysqli_query($con,"SELECT pos from koove_posts");
                    while ($row4 = @mysqli_fetch_array($selectPOS3))
                    {

                        $distinctPOSs.=$row4['pos'].",";        
                    }

                    $distinct_pos_Count = count(array_unique(str_word_count($distinctPOSs, 1))); 
                    //var_dump($distinctPOSs);
                    //echo "Distinv pos:".$distinct_pos_Count.'<br/> ';
            var_dump($POS);
            foreach($POS as $p)
            {
                echo "POS  :".$p."<br/>";
                while ($row1 = @mysqli_fetch_array($selectTag))
                {
                    //Calculate total pos for given tag
                    //echo "Tag : ".$row1['tag']."<br/>";
                    $selectPOS1 = mysqli_query($con,"SELECT * from koove_posts where tag = '".$row1['tag']."'");
                    $totalPOSs = '';
                    while ($row2 = @mysqli_fetch_array($selectPOS1))
                    {
                        //echo $row2['pos']."<br/>";
                        $totalPOSs.=$row2['pos'].",";       
                    }
                    $totalPOS_count = str_word_count($totalPOSs);
                    //echo "Totla POS fo tag ".$row1['tag']." is ".$totalPOS_count."<br/>";

                    //calculate how many times particular 'pos' appears for given tag
                    //echo $totalPOSs."<br/>";
                    echo "pos : ".$p."<br/>";
                    $pos_Count = substr_count($totalPOSs, $p);

                    //echo "POS count for POS ".$p." and  tag ".$row1['tag']." is ".$pos_Count."<br/>";

                    //Calculate the probability for each part of speach
                    $prob[] = (object) array(
                        "value" => ($pos_Count + 1)/ ($totalPOS_count + $distinct_pos_Count),
                        "tag"   => $row1['tag'],
                        );
                        }               
            }
}

更新

$POS循環很好,但問題出在

while ($row1 = @mysqli_fetch_array($selectTag))
                { 

}

因為我注意到了,對於上面的第一部分語言, while循環正確執行,但是對於下一個$POS ,則不能。

我需要刷新/清除$row1嗎?

此代碼可以正常工作。

嘗試注釋循環中的所有內容(僅保留回聲),您將在數組中擁有所有值。

您還應該在代碼的開頭添加:

error_reporting(E_ALL);
ini_set('display_errors','1');

(僅測試時),並刪除@前面mysqli_fetch_array已經顯示所有錯誤

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM