简体   繁体   中英

iterate through string array

For my code: var_dump($POS);

gives me:

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)

But while Iterate through array $POS

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

it echos only first value ie technical

What is wrong?

Actual code:

    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'],
                        );
                        }               
            }
}

UPDATE

loop for $POS is fine but issue is with

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

}

Because what I noticed, For first part of speech above while loop executed correctly, but for next $POS it does not.

Do I need to flush/clear $row1 ?

This code works fine.

Try to comment everything in loop (leave only echo) and you will have all values in your array.

You should also add at the beginning of your code:

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

(only when testing) and remove @ in front of mysqli_fetch_array to have displayed all errors

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM