php - 如何解决 Laravel 中的 csv 文件读取错误
问题描述
这是我使用数组读取 CSV 文件的代码。但它显示了一个名为Undefined offset:1的错误 请任何人说我的代码有什么问题?我还想在 HTML 表格中打印结果。我怎样才能做到这一点?
class ProjectController extends Controller
{
public function index($id)
{
$project = Project::find($id);
$data = array();
// read the CSV and assign the data to $data
$filename = public_path("/uploads/{$id}.csv");
$file = fopen($filename, "r");
$all_data = array();
while (($data = fgetcsv($file, 200, ",")) !== FALSE) {
$Keywords = $data[0];
$Avg_search_volumn = $data[1];
$Top_of_the_page_bid_low = $data[2];
$all_data = $Keywords . " " . $Avg_search_volumn . " " . $Top_of_the_page_bid_low;
array_push($array, $all_data);
}
fclose($file);
echo "<pre>";
print_r($all_data);
die;
return view('home')->with('project', $project)->with('data', $data);
}
}
解决方案
推荐阅读
- android - 将 longClickListener 添加到 ViewGroup 并将单击侦听器添加到 viewgroups 子项-如何使两者都起作用?
- node.js - SequelizeDatabaseError:SQLITE_ERROR:没有这样的表:用户
- python-3.x - BLE 设备在一段时间后断开连接
- node.js - 安装Vue后无法创建Vue项目,怎么办?
- java - 无法让edittext在滚动视图中工作
- c++ - 为什么使用删除会导致核心转储?
- jquery - Jquery替换嵌套范围内的文本
- react-native - React Native:错误:捆绑失败:index.js:“”不是有效的标识符名称
- angularjs - 从Angularjs中的数组中获取特定元素
- c# - 外键关系没有出现在 edmx 与 mysql 数据库与 asp mvc 与数据库第一