首页 > 解决方案 > 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
        ];

标签: phparrays

解决方案


您可以将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 的更多信息

注意:如果两个数组值都没有按照代码中所示的顺序排列,这可能会出现异常。


推荐阅读