在laravel-excel中循环遍历数组中的数组
Posted
技术标签:
【中文标题】在laravel-excel中循环遍历数组中的数组【英文标题】:Looping through an array within an array in laravel-excel 【发布时间】:2015-11-18 05:14:52 【问题描述】:我正在使用 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)
【问题讨论】:
【参考方案1】:您可以按如下方式逐步遍历多维数组:
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']));
【讨论】:
在使用上述代码时,我得到了非法的字符串偏移“状态”。 'state' 是指 $v2['state'],而不是先前的 'state'。我想也许我必须更深一层?我会尝试按照你的模式。 我升级了一个级别,我没有错误,但不幸的是,数据库似乎没有更新。 如果我这样做: foreach ( $dbseeder as $k1 => $v1 ) if ( is_array($v1) ) foreach ( $v1 as $k2 => $v2 ) echo $k2 ;回声 $v2; if (is_array($v2) ) foreach ($v2 as $k3 => $v3 ) DB::table('distributors') ->where('state', $v3['state']) ->update (array('total' => $v3['total'])); 那么 print_r 上的输出是: stateAKtotal0stateALtotal5stateARtotal10stateAZtotal5 等等...我会看@removing【参考方案2】:感谢 Dave 和我自己,以下代码有效:
$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 $k1 => $v1 )
if ( is_array($v1) )
foreach ( $v1 as $k2 => $v2 )
// print_r($k2);
// print_r($v2);
DB::table('distributors')
->where('state', $v1['state'])
->update(array('total' => $v1['total']));
$allstates = DB::table('distributors')->lists('state');
$alltotals = DB::table('distributors')->lists('total');
return View::make('create')
->with(compact('allstates'))
->with(compact('alltotals'));
【讨论】:
以上是关于在laravel-excel中循环遍历数组中的数组的主要内容,如果未能解决你的问题,请参考以下文章