Php阵列无法正常工作。怎么了?

时间:2017-12-17 11:11:10

标签: php arrays for-loop

我想使用api。

这是我的代码:

// create a new purchase bill
$purchase = $parasut->make('sale')->create(array (
    'description'    => $siparis,
    'invoice_id'     => null,
    'invoice_series' => null,
    'currency'       => 'TRL',
    'item_type'      => 'invoice',
    'issue_date'     => $order_date_created,
    'due_date'       => $order_date_created,
    'contact_id'     => $contactToken,
    'category_id'    => null,
    'archived'       => false,
    'billing_address' => $user_address,
    'billing_fax'   => null,
    'billing_phone' => $user_phone,
    'details_attributes' => array (
        $parasut->make('product')->getProductFromOrder(), // the products
    ),
));

这是getProductFromOrder()函数:

public function getProductFromOrder()
{
    $sku = array(5542003,5542004);
    $qty = array("3","1");
    $total = array("11.29","12.00");

    for($i=0; $i<count($sku); $i++){
        $urunler[$i] = array(
                    'product_id'     => $sku[$i], // the products
                    'quantity'       => '3',
                    'unit_price'     => '12.99',
                    'vat_rate'       => '18',
                    'discount_type'  => 'amount',
                    'discount_value' => '0',
            );
        print ($urunler[$i]);
    }
}

它不起作用。当我运行第一个代码时,产品没有显示。错误在哪里?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

将函数getProductFromOrder()改为:

public function getProductFromOrder()
{
    $sku = array(5542003,5542004);
    $qty = array("3","1");
    $total = array("11.29","12.00");

    for($i=0; $i<count($sku); $i++){
        $urunler[$i] = array(
                    'product_id'     => $sku[$i], // the products
                    'quantity'       => '3',
                    'unit_price'     => '12.99',
                    'vat_rate'       => '18',
                    'discount_type'  => 'amount',
                    'discount_value' => '0',
            );
    }
    return $urunler;
}

使用return代替print并在return循环之外写for