gpt4 book ai didi

PHP: undefined offset 为 0,但它就在那里(我认为)

转载 作者:可可西里 更新时间:2023-10-31 23:50:32 25 4
gpt4 key购买 nike

所以我有一个二维数组,由短语和单个单词组成,称为 $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] 时,它会给我:

Notice: Undefined offset: 0 in C:\wamp\www\jpsmythe\parser.php on line 57

Notice: Undefined offset: 0 in C:\wamp\www\jpsmythe\parser.php on line 57

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

最佳答案

添加一个

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

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

关于PHP: undefined offset 为 0,但它就在那里(我认为),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4210131/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com