PHP - 注意未定义的偏移量
PHP - Notice Undefined offset
我的 php 脚本中有一个我不明白的问题。我知道有几个关于这个问题的问题,但 none 适合我的问题。
我实际上有一个用制表符分隔的输入文件,名为 testfile.txt
。
使用此 txt 文件,我创建了一个名为 result.txt 的新文件,其中我在第 0 列和第 7 列中获取了测试文件的内容。
当我执行我的 php 脚本时,我得到这个错误:
Notice: Undefined offset: 7
我不明白的是,我的 result.txt
是用我的 testfile.txt
的第 0 列和第 7 列中包含的数据创建的。如果我这样做:
echo $dataFromTestFile[7];
我在第7列输出内容
所以我真的不明白为什么我有这个通知以及如何删除它。
这是我的 php 脚本:
<?php
if (false !== ($ih = fopen('/opt/lampp/htdocs/ngs/tmp/testfile.txt', 'r'))) {
$oh = fopen('/opt/lampp/htdocs/ngs/tmp/result.txt', 'w');
while (false !== ($dataFromTestFile = fgetcsv($ih,0,"\t"))) {
// this is where I build my new row
$outputData = array($dataFromTestFile[0], $dataFromTestFile[7]);
fputcsv($oh, $outputData);
//echo $dataFromTestFile[7];
}
fclose($ih);
fclose($oh);
}
?>
testfile.txt的示例数据:
Input Errors AccNo Genesymbol Variant Reference Coding Descr. Coding
aaa ddd fdfd dfdf fefefd ref1 fdfdfd fdfdf dfdfde
确保第 7 列存在于您的 testfile.txt - 我想当从零开始时它可能是第 6 列 - 你也可以
var_dump($dataFromTestFile)
为了获取变量的内容 - 数组键和值可能与您的问题有关
我怀疑这是导致错误的行:
$outputData = array($dataFromTestFile[0], $dataFromTestFile[7]);
您正在尝试使用特定索引处的数组元素,而不检查它们是否确实存在。
此外,您正在尝试将数组对象写入结果文件,您的意思是要在该文件中创建一个逗号分隔值吗?
试试这个:
$source = '/opt/lampp/htdocs/ngs/tmp/testfile.txt';
$result = '/opt/lampp/htdocs/ngs/tmp/result.txt';
if (($handle = fopen($source, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if (isset($data[0]) && isset($data[7])) {
file_put_contents($result, $data[0] .','. $data[7] ."\r\n");
}
}
fclose($handle);
}
或者,您也可以像这样将结果写成 csv:
$sourceFile = '/opt/lampp/htdocs/ngs/tmp/testfile.txt';
$resultFile = '/opt/lampp/htdocs/ngs/tmp/result.txt';
$resultData = array();
// Parse source file
if (($handle = fopen($sourceFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if (isset($data[0]) && isset($data[7])) {
$resultData[] = array($data[0], $data[7]);
}
}
fclose($handle);
}
// Write result file
if (sizeof($resultData)) {
$h = @fopen($resultFile, 'w');
if (!$h) {
exit('Failed to open result file for writing.');
}
foreach ($resultData as $resultRow) {
fputcsv($h, $resultRow, ',', '"');
}
fclose($h);
}
我的 php 脚本中有一个我不明白的问题。我知道有几个关于这个问题的问题,但 none 适合我的问题。
我实际上有一个用制表符分隔的输入文件,名为 testfile.txt
。
使用此 txt 文件,我创建了一个名为 result.txt 的新文件,其中我在第 0 列和第 7 列中获取了测试文件的内容。
当我执行我的 php 脚本时,我得到这个错误:
Notice: Undefined offset: 7
我不明白的是,我的 result.txt
是用我的 testfile.txt
的第 0 列和第 7 列中包含的数据创建的。如果我这样做:
echo $dataFromTestFile[7];
我在第7列输出内容
所以我真的不明白为什么我有这个通知以及如何删除它。
这是我的 php 脚本:
<?php
if (false !== ($ih = fopen('/opt/lampp/htdocs/ngs/tmp/testfile.txt', 'r'))) {
$oh = fopen('/opt/lampp/htdocs/ngs/tmp/result.txt', 'w');
while (false !== ($dataFromTestFile = fgetcsv($ih,0,"\t"))) {
// this is where I build my new row
$outputData = array($dataFromTestFile[0], $dataFromTestFile[7]);
fputcsv($oh, $outputData);
//echo $dataFromTestFile[7];
}
fclose($ih);
fclose($oh);
}
?>
testfile.txt的示例数据:
Input Errors AccNo Genesymbol Variant Reference Coding Descr. Coding
aaa ddd fdfd dfdf fefefd ref1 fdfdfd fdfdf dfdfde
确保第 7 列存在于您的 testfile.txt - 我想当从零开始时它可能是第 6 列 - 你也可以
var_dump($dataFromTestFile)
为了获取变量的内容 - 数组键和值可能与您的问题有关
我怀疑这是导致错误的行:
$outputData = array($dataFromTestFile[0], $dataFromTestFile[7]);
您正在尝试使用特定索引处的数组元素,而不检查它们是否确实存在。
此外,您正在尝试将数组对象写入结果文件,您的意思是要在该文件中创建一个逗号分隔值吗?
试试这个:
$source = '/opt/lampp/htdocs/ngs/tmp/testfile.txt';
$result = '/opt/lampp/htdocs/ngs/tmp/result.txt';
if (($handle = fopen($source, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if (isset($data[0]) && isset($data[7])) {
file_put_contents($result, $data[0] .','. $data[7] ."\r\n");
}
}
fclose($handle);
}
或者,您也可以像这样将结果写成 csv:
$sourceFile = '/opt/lampp/htdocs/ngs/tmp/testfile.txt';
$resultFile = '/opt/lampp/htdocs/ngs/tmp/result.txt';
$resultData = array();
// Parse source file
if (($handle = fopen($sourceFile, "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if (isset($data[0]) && isset($data[7])) {
$resultData[] = array($data[0], $data[7]);
}
}
fclose($handle);
}
// Write result file
if (sizeof($resultData)) {
$h = @fopen($resultFile, 'w');
if (!$h) {
exit('Failed to open result file for writing.');
}
foreach ($resultData as $resultRow) {
fputcsv($h, $resultRow, ',', '"');
}
fclose($h);
}