所以我有一个二维数组,由短语和单词组成,称为 $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]
时,它给了我:这是没有意义的。我觉得我已经尝试了一切,包括将 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]);
}
最佳答案
添加一个if (isset($frags[$i][0])) {
到解析器函数的顶部似乎已经修复了它。这很公平,但我仍然不完全确定我明白了。
关于PHP:未定义偏移量为 0,但它在那里(我认为),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4210131/