php - PHP 将一个 $ 数量的数组映射到一个变量数组,并将变量乘以它的正确数量
问题描述
我有一个变量数组,每个变量都乘以特定数量。我正在寻找这个地图,所以价格会更容易更新。关于最好的方法的任何建议?
$charges = [
$row->StandardConcretePanel * $standardConcretePanelCharge,
$row->ADAPedestrianRampWithDomes * 1200,
$row->ADAPedestrianRampNoDomes * 800,
$row->OversizedConcretePanel * 340,
$row->ThickConcretePanel8to10 * 320,
$row->ThickConcretePanel10to12 * 340,
$row->ThickConcretePanel12orMore * 360,
$row->HighEarlyConcretePanels * 1100,
$row->WinterMix * 75,
$row->ConcreteBlanketInstallation * 50,
$row->PanelRemovalFillWithClassV * $panelRemovalCharge,
$row->BarricadedAndProtectedPanels * 20,
$row->HighProfilePanelsFrontOfBusinesses * 1100,
$row->BeforeOrAfterHoursPanels * 1500,
$row->AlleyOrDrivewayPanelWithIntegralCurb * 1200,
$row->ConcreteShortLoads4PanelsOrLess * 400,
$row->CurbAndGutter * 1200
];
解决方案
您可以将array_map与回调函数一起使用
$data = $row;
//Make array which contains all of charges
$charges = array(1200, 800, so on...);
$result = array_map(function ($d, $c){
return $d * $c ;
},$data, $charges);
$var_dump($result);
//Now $result have all of your multiplied result.
阅读有关array_map 的更多信息
注意:如果两个数组值都没有按照代码中所示的顺序排列,这可能会出现异常。
推荐阅读
- java - 如何在 Java 中使用 service-now Table API?
- ruby-on-rails - 如何将此haml片段转换为html.erb代码
- angular - 如何在 ag-grid 的无限行模型中实现 pageSize 更改?
- c++ - node.js 到 c++ 协议缓冲区
- r - 将列表 A 中数据框中的最后一个值与列表 B 中数据框中的第一个值相加
- android - Volley 库的问题
- javascript - Why Am I Getting An Expected an Assignment or Function Call and Instead Saw an Expression?
- javascript - 禁用 v-autocomplete 中的选项
- ruby-on-rails - 尝试显示图像时,出现错误“404 not found”
- assembly - AVR 汇编:jmp 和 rjmp 指令中的地址是什么?