Laravel异常:两个参数应具有相等数量的元素

时间:2019-10-23 18:06:07

标签: php laravel

我是laravel的新手,我正在制作一个需要将.CSV数据导入数据库的应用程序,该数据已成功导入,但是我遇到了这个问题:

ErrorException array_combine(): Both parameters should have an equal number of elements

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Accounts;
class AccountController extends Controller
{
    public function show(){
        return view ('upload');
    }
    public function store(Request $request){


        $file = $request->file('upload-file');
        $csvData = file_get_contents($file);

        $rows = array_map("str_getcsv", explode("\n", $csvData));
    // dd($rows);
        $header = array_shift($rows);
//    dd($header);
        foreach ($rows as $row) {

            $row = array_combine($header, $row);
            if (count($header) != count($row)) {
                continue;
              }
            set_time_limit(0);
            Accounts::create([
                'AccountClass' => $row['Classe'],
                'AccountNumber' => $row['Compte'],
                'AccountDesc' => $row['Desc'],
                'active' => 1,
            ]);
        }

        return view ('home');

    }

}

您能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:0)

当您尝试合并两个长度不相等的数组时,会出现此错误。

$arr1 = ["a", "s", "d"];
$arr2 = [1, 2, 3];

if(count($arr1) == count($arr2)){
    $result = array_combine($arr1, $arr2);
} else{
    echo "Error array combine";
}