在PHP中,从文件中分解一行会产生"未定义偏移"错误



我正在尝试逐行读取文件($txtFile(。然后将不同的项目存储在数组($pieces(中用逗号分隔的行($line(中。代码如下所示。

<?php
include_once __DIR__.'/connect.php';
function createURL($ticker)
{
$currentPeriod = time(); 
return "https://query1.finance.yahoo.com/v7/finance/download/$ticker?period1=1538917807&period2=1541596207&interval=1d&events=history&crumb=6DH0b71wPwr";
}
function getCSVFile($url, $outputFile)
{
$content = file_get_contents($url);
$content = str_replace("Date,Open,High,Low,Close,Adj Close,Volume", "", $content);
$content = trim($content);
file_put_contents($outputFile, $content);
}
function fileToDatabase($txtFile, $tableName)
{
$file = fopen($txtFile, "r");
while (!feof($file)) {
$line = fgets($file);
$pieces = explode(",", $line);
print_r($pieces);
$date = $pieces[0];
$open = $pieces[1];
$high = $pieces[2];
$low = $pieces[3];
$close = $pieces[4];
$adj_close = $pieces[5];
$volume = $pieces[6];
$amount_change = $adj_close-$open;
$percent_change = ($amount_change/$open)*100;
$sql = "SELECT * FROM $tableName";
$result = $conn->query($sql);
if (!$result) {
$createQuery = "CREATE TABLE $tableName (
date TIMESTAMP PRIMARY KEY,
open FLOAT,
high FLOAT,
low FLOAT,
close FLOAT,
adj_close FLOAT,
volume INT,
amount_change FLOAT,
percent_change FLOAT
)";
$conn->query($createQuery);
}
$insertQuery = "INSERT INTO $tableName VALUES (
'$date', '$open', '$high', '$low', '$close', '$adj_close', '$volume', '$amount_change', '$percent_change'
)";
$conn->query($insertQuery);
}
fclose($file);
}
function main()
{
$pathToTickerFile = __DIR__."/tickerMaster.txt";
$mainTickerFile = fopen($pathToTickerFile, "r");
while (!feof($mainTickerFile)) {
$companyTicker = fgets($mainTickerFile);
$companyTicker = trim($companyTicker);
$fileURL = createURL($companyTicker);
$companyTxtFile = __DIR__."/txtFiles/".$companyTicker.".txt";
getCSVFile($fileURL, $companyTxtFile);
fileToDatabase($companyTxtFile, $companyTicker);
}
fclose($mainTickerFile);
echo "The stocks have been downloaded!";
}
main();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Stock Downloader</title>
</head>
<body>
</body>
</html>

但我得到了以下错误:

PHP Notice:  Undefined offset: 1
PHP Notice:  Undefined offset: 2
PHP Notice:  Undefined offset: 3
PHP Notice:  Undefined offset: 4
PHP Notice:  Undefined offset: 5
PHP Notice:  Undefined offset: 6

我正在使用PHP-7.0&我无法理解此代码出了什么问题。如何消除此错误?

一个快速修复方法是检查是否有一行包含7个元素,因此在分解后,只需检查数组中有多少元素,如果没有足够的值,则跳过其余的处理。。。

$pieces = explode(",", $line);
if ( count($pieces) != 7)   {
continue;
}
$date = $pieces[0];
// ....

您还应该考虑使用prepared语句,因为它提供了更多的安全性,尽管您仍然需要像现在这样替换表名(不能将表名作为绑定变量(。

此外,建议在执行INSERT时,始终列出列名。。。

INSERT INTO $tableName (date, open, ... )
VALUES ('$date', '$open', ...)

因为这可以确保清楚地知道哪一列在哪里,如果对表进行了任何更改,那么INSERT就明确地说明了它对哪些列使用了哪些值。

您的文件包含一个空行(或可能包含多个(。你通常会在文件的最后看到这个。

因此,explode()返回一个具有单个空值的数组,而您需要7(键0到6(。

在将行分解为数组之前,请检查它是否确实为空。或者在创建数组后对其进行计数,并验证是否存在预期的键数。

在索引到数组之前,您可以检查$pieces中的项数,例如,如果爆炸的项数返回:

$file = fopen($txtFile, "r");
while (!feof($file)) {
$line = fgets($file);
$pieces = explode(",", $line);
if (count($pieces) > 6) {
$date = $pieces[0];
$open = $pieces[1];
$high = $pieces[2];
$low = $pieces[3];
$close = $pieces[4];
$adj_close = $pieces[5];
$volume = $pieces[6];
}
}

最新更新