文件$ wordFile [超过数百行]的每一行,由一个单词+'空格'组成。 +十进制数+' \ n'。
我需要从文件中提取数字和单词并将它们添加到$ indivFreq数组中。但是我得到了错误
注意:未定义的偏移:在/.....
中为1这是我的代码:
$wordFile = file_get_contents("wordFile.txt");
$termArr = explode("\n", $wordFile);
$wordFile2 = file_get_contents("wordFile2.txt");
$termArr2 = explode("\n", $wordFile2);
$cityFreqsArr = array($termArr, $termArr2);
$twoTerms = array();
$decimalScore = number_format("0", 8);
$indivFreqs = array("", $decimalScore);
$twoDimFreqArr = [[]];
for($i=0; $i<count($termArr); $i++){
for($i2=0; $i2 < count($cityFreqsArr[$i]); $i2++){
$currentTerm = $cityFreqsArr[$i][$i2];
$currentTerm = trim($cityFreqsArr[$i][$i2]);
$twoTerms = explode(' ', $currentTerm); //separating each string of term and its frequency into the 2 parts
$indivFreqs[0] = $twoTerms[0];
$indivFreqs[1] = $twoTerms[1]; //**error is here
$twoDimFreqArr[$i][$i2] = $indivFreqs; //for each city and each term there is an array with the term and its frequency
//i.e.,
}
}
我尝试在爆炸后打印出$ twoTerms的内容,并获得索引为0 =&gt;的数组。单词和索引1 =&gt;数字,这就是为什么我不明白为什么php不会看到它的索引也是1?
答案 0 :(得分:0)
您已将$cityFreqsArr
定义为二维数组,其行数等于正在读取的文件(对于此特定示例,该行数为2.第一行($index=0
)持有$wordFile.txt
/ $termArr
的数据。第二行是第二行。
现在存储每个城市的数据,每个术语和各自的术语和您首先应该通过其行(作为文件)&amp;遍历$cityFreqsArr
遍历频率。然后是第二级$index
(作为每个文件的内容)。所以,我先更新了&amp;关于此问题的第二个for
条件。
$wordFile = file_get_contents("wordFile.txt");
$termArr = explode("\n", $wordFile);
$wordFile2 = file_get_contents("wordFile2.txt");
$termArr2 = explode("\n", $wordFile2);
$cityFreqsArr = array($termArr, $termArr2);
$twoTerms = array();
$decimalScore = number_format("0", 8);
$indivFreqs = array("", $decimalScore);
$twoDimFreqArr = [[]];
for ($i = 0; $i < count($cityFreqsArr); $i++) { //Traversing file by file
for ($i2 = 0; $i2 < count($cityFreqsArr[$i]); $i2++) { //Traversing in each file
$twoTerms = explode(' ', trim($cityFreqsArr[$i][$i2]));
$indivFreqs[] = $twoTerms[0];
$indivFreqs[] = $twoTerms[1];
$twoDimFreqArr[$i][$i2] = $indivFreqs;
}
}
// Test it:
echo '<pre>';
print_r($twoDimFreqArr);
echo '</pre>';