数组键有问题,未定义索引

时间:2013-03-28 19:52:27

标签: php arrays

我正在使用几个数组,其中一个数组的键是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);
    }

非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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--;