首页 > 解决方案 > 如何解决 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);
   }
  }

标签: phparrayslaravelhtml-tablephpmyadmin

解决方案


推荐阅读