我怎样才能避免类似的foreach条件?

时间:2015-09-09 11:34:41

标签: php arrays foreach iterator

我有一个代码,我正在使用多个数组和foreach语句。大多数代码非常相似,所以我想改变这些代码行,但我很无能

$requiredProducts = Array
(
    [62861] => Array
        (
            [0] => 6910
            [1] => 17118
            [2] => 32218
            [3] => 39134
            [4] => 39154
            [5] => 39156
        )

    [57590] => Array
        (
            [0] => 39156
            [1] => 39192
            [2] => 39200
        )

)

foreach ($requiredProducts as $vehicleId => $data) {
    foreach ($data as $key => $productId) {
        $webOrderVehicleProductId = (!empty($currentProducts[$vehicleId][$productId])) ?
            $currentProducts[$vehicleId][$productId] : null;
        $products[$vehicleId][] = $this->prepareProduct(
            $webOrderVehicleProductId,
            $productId,
            $this->getConfig()->constant->default->PRODUCT_QUANTITY,
            isset($priceInfo['productsInfo']['productWithInfo'][$productId]['price']) ?
                $priceInfo['productsInfo']['productWithInfo'][$productId]['price'] : 0,
            $this->getConfig()->constant->default->ARCHIVE_STATUS
        );
    }
}

$transitPlates = Array
(
    [62861] => 545,
    [57590] => 848
)

foreach ($transitPlates as $vehicleId => $transitPlateId) {
    $webOrderVehicleProductId = (!empty($currentProducts[$vehicleId][$transitPlateId])) ?
        $currentProducts[$vehicleId][$transitPlateId] : null;
    $products[$vehicleId][] = $this->prepareProduct(
        $webOrderVehicleProductId,
        $transitPlateId,
        $this->getConfig()->constant->default->PRODUCT_QUANTITY,
        isset($priceInfo['productsInfo']['transitPlateWithInfo'][$transitPlateId]['price']) ?
                $priceInfo['productsInfo']['transitPlateWithInfo'][$transitPlateId]['price'] : 0,
        $this->getConfig()->constant->default->ARCHIVE_STATUS
    );
}

$dieselProducts = Array
(
    [62861] => Array
        (
            [39156] => 200
            [39192] => 96
        )
)

$dieselProductId = Basweb_Shared_Model_Entity_Product::PRODUCT_DIESEL_ID;
    foreach ($dieselProducts as $vehicleId => $data) {
        $webOrderVehicleProductId = (!empty($currentProducts[$vehicleId][$dieselProductId])) ?
            $currentProducts[$vehicleId][$dieselProductId] : null;
        $products[$vehicleId][] = $this->prepareProduct(
            $webOrderVehicleProductId,
            $dieselProductId,
            $data[$dieselProductId]['quantity'],
            isset($priceInfo['dieselPrice']) ? $priceInfo['dieselPrice'] : 0,
            $this->getConfig()->constant->default->ARCHIVE_STATUS
    );
}

$tyres = Array
(
    [62861] => Array
        (
            [front_1] => 39156
            [rare_1] => 39192
        )
)

foreach ($tyres as $vehicleId => $tyre) {
    foreach ($tyre as $axle => $productId) {
        $tyreOffers = isset($priceInfo['tyreInfo'][$vehicleId][$axle]) ?
            $priceInfo['tyreInfo'][$vehicleId][$axle] : array();
        $webOrderVehicleProductId = (!empty($currentProducts[$vehicleId][$productId][$axle])) ?
            $currentProducts[$vehicleId][$productId][$axle] : null;
        $products[$vehicleId][] = $this->prepareProduct(
            $webOrderVehicleProductId,
            $productId,
            $this->getConfig()->constant->default->TYRE_QUANTITY,
            $this->getTyrePrice($productId, $tyreOffers),
            $this->getConfig()->constant->default->ARCHIVE_STATUS,
            $axle
        );
    }
}

我尝试搜索递归数组迭代器以避免多次foreach但到目前为止没有运气。任何帮助将不胜感激。

0 个答案:

没有答案