0

我正在使用几个数组,其中一个具有作为 productid 的键,值是包含该产品的学说实体的对象。同样,我有一个索引为计划 ID 的计划。对于这个 for 循环,我需要为每个计划-产品对检查并单独设置发票(它们之间的数量将始终相同,除非用户正在为已经拥有的设备购买计划)。显然这有点复杂,所以我不确定如何表达这个问题。注释行出现错误,“注意:未定义索引”

for ($i = 0; $i < $totalInvoices; $i++) {
        if ($i == $planQuantity[key($plans)]) {
            next($plans);
        }
        if ($i == $productQuantity[key($products)]) {
            next($products);
        }
        $data = array(
            'planId' => key($plans),
//below here, I'm thinking at key($products) is where the error occurs
            'productId' => key($products) != 0 ? key($products) : $plans[key($plans)]->getPricingTier()->getProductId(),
            'userId' => $this->userId,
            'paymentMethodId' => $paymentMethodId
        );
        if ($order['hasProduct'] || isset($order['activating'])) {
            if (!isset($order['activating'])) {
                $planModel->createDevicePlan($data);
                $productAmount = $products[key($products)]->getAmount();
            } else {
                $data['deviceId'] = $order['activating']['inventoryId'];
                $planModel->createDevicePlan($data, date('Y-m-d'));
                $productAmount = 0;
            }
        } else {
            $productAmount = 0;
        }
        if ($iteration % 5 == 0 && $order['hasShipping']) {
            $invoiceShippingAmount = $billingModel->getShippingAmount($shippingOptionsId);
        } else {
            $invoiceShippingAmount = 0;
        }
        $salesData = array(
            'paymentMethodsId'    => $paymentMethodId,
            'planAmount'          => $plans[key($plans)]->getAmount(),
            'planId'              => key($plans),
            'productAmount'       => $productAmount,
            'productId'           => key($products),
            'shippingAddressesId' => $shippingAddressId,
            'shippingAmount'      => $invoiceShippingAmount,
            'shippingOptionsId'   => $shippingOptionsId,
            'transactionId'       => $transactionId,
            'userId'              => $this->userId
        );
        $iteration++;
        $billingModel->createInvoice($salesData);
    }

任何帮助将不胜感激。

4

1 回答 1

0

根据您的评论回复,我认为这会有所帮助

$plans_on_invoice = 0;
$products_on_invoice = 0;
for ($i = 0; $i < $totalInvoices; $i++) {
    next($plans);
    if ($plans_on_invoice == 0) {
        $plans_on_invoice = $planQuantity[key($plans)];
    }
    plans_on_invoice--;
    next($products);
    if ($products_on_invoice == 0) {
        $products_on_invoice = $productQuantity[key($products)];
    }
    products_on_invoice--;
于 2013-03-28T20:26:05.483 回答