PHP:Undefined Offset为0,但它在那里(我认为)

时间:2010-11-17 23:07:42

标签: php undefined offset

所以我有一个二维数组,短语和单个单词,称为$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

     

string(3)“the”

这没有意义。我觉得我已经尝试了所有的东西,包括将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]);
}

5 个答案:

答案 0 :(得分:0)

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

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

print_r($frags);
  

数组([0] =&gt;数组([0] =&gt; [1]   =&GT; term [2] =&gt; 'AI'[3] =&gt;仍然[4] =&gt;管理)[1] =&gt;数组([0] =&gt;到[1] =&gt;蠕变)[2] =&gt;数组([0] =&gt;   进入[1] =&gt;我的[2] =&gt;演讲)[3] =&gt;   数组([0] =&gt;或[1] =&gt;写。))

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

数组([0] =&gt; [1] =&gt; term [2] =&gt;   'AI'[3] =&gt;仍然[4] =&gt;管理)

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

答案 1 :(得分:0)

添加

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

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

答案 2 :(得分: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

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

答案 3 :(得分:0)

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

我建议你在解析调用之前使用vardump或print_r $子句。

另外,我怀疑哪个[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呕吐的信息。

答案 4 :(得分: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];