有没有更短更美的替代方法?目前正在使用Maatwebsite / Laravel-Excel
foreach ($rows as $key => $value)
{
if($value->{'red'} == 1)
{
DB::table('furnitures')->insert(
[
'chair' => $value->{'Chair Name'},
'desk' => $value->{'Desk Name'},
'tv' => $value->{'TV Name'},
'lamp' => $value->{'Lamp Name'}
'carpet' => 1,
]);
}
if($value->{'green'} == 1)
{
DB::table('furnitures')->insert(
[
'chair' => $value->{'Chair Name'},
'desk' => $value->{'Desk Name'},
'tv' => $value->{'TV Name'},
'lamp' => $value->{'Lamp Name'}
'carpet' => 2,
]);
}
if($value->{'blue'} == 1)
{
DB::table('furnitures')->insert(
[
'chair' => $value->{'Chair Name'},
'desk' => $value->{'Desk Name'},
'tv' => $value->{'TV Name'},
'lamp' => $value->{'Lamp Name'}
'carpet' => 3,
]);
}
}
请注意,'地毯'会根据颜色(红色,绿色,蓝色)更改值。你必须问“为什么所有条件都是,如果不是,为什么不是?”这是因为我想用不同的“地毯”值存储所有值,而其他所有值都是相同的
答案 0 :(得分:1)
请尝试这样的事情:
$colors = ['red' => 1, 'green' => 2, 'blue' => 3];
foreach ($rows as $key => $value) {
foreach ($colors as $color_name => $color_value) {
if ($value->{$color_name} == 1) {
DB::table('furnitures')->insert(
[
'chair' => $value->{'Chair Name'},
'desk' => $value->{'Desk Name'},
'tv' => $value->{'TV Name'},
'lamp' => $value->{'Lamp Name'},
'carpet' => $color_value,
]
);
}
}
}