我正在使用laravel-excel尝试上传.csv文件,然后遍历上传的文件以更新数据库。我认为正在进行的是laravel-excel在数组中创建一个数组,我不知道如何从第二级数组检查和检索。反正我是否可以通过我的方式获取此代码来更新数据库?提前谢谢。
我的控制器:
public function store()
{
$csv = Input::file('csv');
$destinationPath = public_path().'/lists';
// If the uploads fail due to file system, you can try doing public_path().'/uploads'
$filename = $csv->getClientOriginalName();
// $extension = $csv->getClientOriginalExtension();
$upload_success = Input::file('csv')->move($destinationPath, $filename);
Excel::load(public_path().'/lists/states.csv', function($reader) {
// reader methods
$dbseeder = $reader->select(array('state', 'total'))->get()->toArray();
foreach ($dbseeder as $state => $total){
DB::table('distributors')
->where('state', $state)
->update(array('total' => $total));
}
print_r($dbseeder);
});
Print_R($ dbseeder)产生以下结果。如果我做print_r($ reader)那么就会产生一堆其他值(都与Maatwebsite的工具有关):
Array ( [0] => Array ( [state] => AK [total] => 0 ) [1] => Array ( [state] => AL [total] => 5 ) [2] => Array ( [state] => AR [total] => 10 ) [3] => Array ( [state] => AZ [total] => 5 ) [4] => Array ( [state] => CA [total] => 10 ) [5] => Array ( [state] => CO [total] => 11 ) [6] => Array ( [state] => CT [total] => 15 ) [7] => Array ( [state] => DC [total] => 16 ) [8] => Array ( [state] => DE [total] => 20 ) [9] => Array ( [state] => FL [total] => 21 ) [10] => Array ( [state] => GA [total] => 1 ) [11] => Array ( [state] => HI [total] => 0 ) [12] => Array ( [state] => IA [total] => 4 ) [13] => Array ( [state] => ID [total] => 5 ) [14] => Array ( [state] => IL [total] => 2 ) [15] => Array ( [state] => IN [total] => 6 ) [16] => Array ( [state] => KS [total] => 9 ) [17] => Array ( [state] => KY [total] => 10 ) [18] => Array ( [state] => LA [total] => 11 ) [19] => Array ( [state] => MA [total] => 17 ) [20] => Array ( [state] => MD [total] => 22 ) [21] => Array ( [state] => ME [total] => 33 ) [22] => Array ( [state] => MI [total] => 11 ) [23] => Array ( [state] => MN [total] => 2 ) [24] => Array ( [state] => MO [total] => 3 ) [25] => Array ( [state] => MS [total] => 7 ) [26] => Array ( [state] => MT [total] => 44 ) [27] => Array ( [state] => NC [total] => 14 ) [28] => Array ( [state] => ND [total] => 13 ) [29] => Array ( [state] => NE [total] => 7 ) [30] => Array ( [state] => NH [total] => 17 ) [31] => Array ( [state] => NJ [total] => 18 ) [32] => Array ( [state] => NM [total] => 19 ) [33] => Array ( [state] => NV [total] => 21 ) [34] => Array ( [state] => NY [total] => 0 ) [35] => Array ( [state] => OH [total] => 44 ) [36] => Array ( [state] => OK [total] => 55 ) [37] => Array ( [state] => OR [total] => 7 ) [38] => Array ( [state] => PA [total] => 12 ) [39] => Array ( [state] => RI [total] => 3 ) [40] => Array ( [state] => SC [total] => 2 ) [41] => Array ( [state] => SD [total] => 8 ) [42] => Array ( [state] => TN [total] => 15 ) [43] => Array ( [state] => TX [total] => 16 ) [44] => Array ( [state] => UT [total] => 20 ) [45] => Array ( [state] => VA [total] => 21 ) [46] => Array ( [state] => VT [total] => 26 ) [47] => Array ( [state] => WA [total] => 33 ) [48] => Array ( [state] => WI [total] => 13 ) [49] => Array ( [state] => WV [total] => 12 ) [50] => Array ( [state] => WY [total] => 8 ) )
当我在dbseeder上执行var_dump时,我得到:
array(51) { [0]=> array(2) { ["state"]=> string(2) "AK" ["total"]=> float(0) } [1]=> array(2) { ["state"]=> string(2) "AL" ["total"]=> float(5) } [2]=> array(2) { ["state"]=> string(2) "AR" ["total"]=> float(10) } [3]=> array(2) { ["state"]=> string(2) "AZ" ["total"]=> float(5) } [4]=> array(2) { ["state"]=> string(2) "CA" ["total"]=> float(10) } [5]=> array(2) { ["state"]=> string(3) "CO " ["total"]=> float(11) } [6]=> array(2) { ["state"]=> string(2) "CT" ["total"]=> float(15) } [7]=> array(2) { ["state"]=> string(3) "DC " ["total"]=> float(16) } [8]=> array(2) { ["state"]=> string(4) "DE " ["total"]=> float(20) } [9]=> array(2) { ["state"]=> string(3) "FL " ["total"]=> float(21) } [10]=> array(2) { ["state"]=> string(3) "GA " ["total"]=> float(1) } [11]=> array(2) { ["state"]=> string(3) "HI " ["total"]=> float(0) } [12]=> array(2) { ["state"]=> string(3) "IA " ["total"]=> float(4) } [13]=> array(2) { ["state"]=> string(2) "ID" ["total"]=> float(5) } [14]=> array(2) { ["state"]=> string(3) "IL " ["total"]=> float(2) } [15]=> array(2) { ["state"]=> string(4) "IN " ["total"]=> float(6) } [16]=> array(2) { ["state"]=> string(3) "KS " ["total"]=> float(9) } [17]=> array(2) { ["state"]=> string(3) "KY " ["total"]=> float(10) } [18]=> array(2) { ["state"]=> string(3) "LA " ["total"]=> float(11) } [19]=> array(2) { ["state"]=> string(4) "MA " ["total"]=> float(17) } [20]=> array(2) { ["state"]=> string(3) "MD " ["total"]=> float(22) } [21]=> array(2) { ["state"]=> string(3) "ME " ["total"]=> float(33) } [22]=> array(2) { ["state"]=> string(3) "MI " ["total"]=> float(11) } [23]=> array(2) { ["state"]=> string(3) "MN " ["total"]=> float(2) } [24]=> array(2) { ["state"]=> string(3) "MO " ["total"]=> float(3) } [25]=> array(2) { ["state"]=> string(3) "MS " ["total"]=> float(7) } [26]=> array(2) { ["state"]=> string(3) "MT " ["total"]=> float(44) } [27]=> array(2) { ["state"]=> string(3) "NC " ["total"]=> float(14) } [28]=> array(2) { ["state"]=> string(3) "ND " ["total"]=> float(13) } [29]=> array(2) { ["state"]=> string(3) "NE " ["total"]=> float(7) } [30]=> array(2) { ["state"]=> string(3) "NH " ["total"]=> float(17) } [31]=> array(2) { ["state"]=> string(3) "NJ " ["total"]=> float(18) } [32]=> array(2) { ["state"]=> string(3) "NM " ["total"]=> float(19) } [33]=> array(2) { ["state"]=> string(3) "NV " ["total"]=> float(21) } [34]=> array(2) { ["state"]=> string(3) "NY " ["total"]=> float(0) } [35]=> array(2) { ["state"]=> string(3) "OH " ["total"]=> float(44) } [36]=> array(2) { ["state"]=> string(3) "OK " ["total"]=> float(55) } [37]=> array(2) { ["state"]=> string(3) "OR " ["total"]=> float(7) } [38]=> array(2) { ["state"]=> string(3) "PA " ["total"]=> float(12) } [39]=> array(2) { ["state"]=> string(3) "RI " ["total"]=> float(3) } [40]=> array(2) { ["state"]=> string(3) "SC " ["total"]=> float(2) } [41]=> array(2) { ["state"]=> string(3) "SD " ["total"]=> float(8) } [42]=> array(2) { ["state"]=> string(3) "TN " ["total"]=> float(15) } [43]=> array(2) { ["state"]=> string(3) "TX " ["total"]=> float(16) } [44]=> array(2) { ["state"]=> string(3) "UT " ["total"]=> float(20) } [45]=> array(2) { ["state"]=> string(3) "VA " ["total"]=> float(21) } [46]=> array(2) { ["state"]=> string(3) "VT " ["total"]=> float(26) } [47]=> array(2) { ["state"]=> string(3) "WA " ["total"]=> float(33) } [48]=> array(2) { ["state"]=> string(3) "WI " ["total"]=> float(13) } [49]=> array(2) { ["state"]=> string(3) "WV " ["total"]=> float(12) } [50]=> array(2) { ["state"]=> string(3) "WY " ["total"]=> float(8) } }
答案 0 :(得分:0)
您可以按如下步骤浏览多维数组:
foreach ( $dbseeder as $k1 => $v1 ) {
if ( is_array($v1) ) {
foreach ( $v1 as $k2 => $v2 ) {
/**
* This gives you access to $v2['state'] and $v2['total']
*/
DB::table('distributors')
->where('state', $v2['state'])
->update(array('total' => $v2['total']));
}
}
}