4

所以我有一个由短语和单个单词组成的二维数组,称为$frags'var_dump',如下所示:

array(4) { 
[0]=> array(5) { 
    [0]=> string(3) "the" [1]=> string(4) "term" [2]=> string(4) "'AI'" [3]=> string(5) "still" [4]=> string(7) "manages" } 
[1]=> array(2) { 
    [0]=> string(2) "to" [1]=> string(5) "creep" } 
[2]=> array(3) { 
    [0]=> string(4) "into" [1]=> string(2) "my" [2]=> string(6) "speech" } 
[3]=> array(2) { 
    [0]=> string(2) "or" [1]=> string(8) "writing." } 
}  

当我var_dump($frags[0])给我:

array(5) { 
    [0]=> string(3) "the" [1]=> string(4) "term" [2]=> string(4) "'AI'" [3]=> string(5) "still" [4]=> string(7) "manages" } 

但是当我尝试打印时$frags[0][0],它给了我:

注意:未定义的偏移量:第 57 行 C:\wamp\www\jpsmythe\parser.php 中的 0

注意:未定义的偏移量:第 57 行 C:\wamp\www\jpsmythe\parser.php 中的 0

字符串(3)“的”

这是没有意义的。我觉得我已经尝试了所有方法,包括将 0 视为字符串,但它仍然没有看到它。帮助?

相关代码:

private function clause($frags) {
   // set the parser through the control hub
    $controller = Controller::getInstance();
    $parser = $controller->parser;
    $parser->init();

   // take the $frags array from elsewhere in the program
   // this $frags is actually punctuation-separated fragments of a sentence, not the  same as the $frags array with which I'm having problems now   
    $i = 0;
    $clauses = array();
   //run through the punctuated $frags array
    while ($i < count($frags)) {
        $pos = array();
        $num = 0;
        $p = 0;
// separated each punc'ed fragment into smaller fragments separated by stopwords        
        while ($p < count($frags[$i])) {
            if (array_key_exists($frags[$i][$p], $parser->stopwords)) {
                $pos[] = $p;
                $clauses[$num] = array();
                $num ++;
            }
            $p ++;
        }
        $pos[] = count($frags[$i]);

        $num = 0;
        $j = 0;
        if (count($pos) > 0) {
            while ($j < count($pos)) {
                $stop = FALSE;
                $k = $pos[$j]-1;
                while ($k >= 0 && !$stop) {
                    if (array_key_exists($frags[$i][$k], $parser->stopwords))
                        $stop = TRUE;
                    $clauses[$num][] = $frags[$i][$k];
                    $k --;
                }
                
                $clauses[$num] = array_reverse($clauses[$num]);
                $num ++;
                $j ++;
            }

           //send the array of stopped fragments to the parser
            $parser->parse($clauses);
            //$controller->setResponse($clauses);
        }
        $i ++;
    }
}

function parse($frags) {
    $i = 0;
    
    // more code here, commented out for the time being
    // below, send response to control hub for output
    $controller = Controller::getInstance();
    $controller->setResponse($frags[$i][0]);
}
4

5 回答 5

1

添加一个

if (isset($frags[$i][0])) {

到解析器函数的顶部似乎已经修复了它。这很公平,但我仍然不完全确定我明白了。

于 2010-11-18T01:49:13.580 回答
0
$grabs = array(0=> array(0=>"the",1=>"term",2=>"AI",3=> "still",4=> "manages"),
                1=> array(0=>"to",1=>"creep"),
                2=> array(0=>"into", 1=>"my" ,2=>"speech"),
                3=> array(0=> "or",1=>"writing")
                );
 echo $grabs[0][0];
于 2010-12-18T13:39:49.150 回答
0

该值$frags是在您粘贴的代码之外声明的(可能在调用 ::clause() 的调用范围内)。这经常发生在变量名冲突中。考虑一下:

<?php
$frags = array('apple','orange');
// other stuff
while ($frags = $query->next()) {
  //...
}
// the last value returned from $query->next() was false in this example
// .. more stuff

$this->clause($frags); // actually passes false, not the apple,orange array

这发生在我们最好的人身上(-:

于 2010-12-11T21:44:59.237 回答
0

在不知道$parser->stopwords是什么的情况下,很难运行您的样本以查看其失败之处。但我的第一个观察结果是,您没有将 $frags 传递给 parse(),而是传递了 $clauses。并且您可能会通过将 $frags 转换为 $clauses 的循环,而无需将任何内容分配给 $clauses[0]。

我建议您在调用解析之前使用 vardump 或 print_r $clauses。

另外,我质疑哪个 [0] 失败了?第一个还是第二个?简单的判断方法......在解析中执行此操作:

function parse($frags) {
    $i = 0;

    // more code here, commented out for the time being
    // below, send response to control hub for output
    $controller = Controller::getInstance();

    if (!isset($frags[$i])) echo "failure at ".__LINE__." [$i]\n";
    if (!isset($frags[$i][0])) echo "failure at ".__LINE__." [$i][0]\n";

    $controller->setResponse($frags[$i][0]);
}

这应该是关于哪个索引导致 php 呕吐的信息。

于 2010-12-16T06:49:46.250 回答
0

这对我来说很好......

$frags = array(array ('the', 'term', '\'AI\'', 'still', 'manages'), array ('to', 'creep'), array ('into', 'my', 'speech'), array ('or', 'writing.'));

print_r($frags);

数组 ( [0] => 数组 ( [0] => [1] => 术语 [2] => 'AI' [3] => 仍然 [4] => 管理 ) [1] => 数组 ( [ 0] => 到 [1] => 蠕变)[2] => 数组([0] => 进入 [1] => 我的 [2] => 演讲)[3] => 数组([0] =>或 [1] => 写作。))

print "<br>";
print_r($frags[0]);

数组( [0] => [1] => 术语 [2] => 'AI' [3] => 仍然 [4] => 管理)

print "<br>";
print $frags[0][0];

于 2010-11-17T23:38:02.507 回答