如何将变量值插入数组键?

时间:2014-06-11 07:13:11

标签: php arrays variables

我有这段代码:

<?php
require_once ('mercadopago.php');

$monto = $_POST['amount'];

$mp = new MP('XXXX', 'XXXXXXXXXXX');

$preference_data = array(
    "items" => array(
       array(
           "title" => "item",
           "quantity" => 1,
           "currency_id" => "usd",
           "unit_price" => HERE
       )
    )
);

$preference = $mp->create_preference ($preference_data);

?>

我需要制作我发布之前定义的变量$ monto,以便将其值赋予&#34; Unit_price&#34;在哪里说&#34; HERE&#34;。我试着写$ monto,但它没有用。

有一些我怎么做到这一点?谢谢你,对不起我的英语。它不太好。

2 个答案:

答案 0 :(得分:2)

您可以使用变量,例如:

//check if your POST data is not empty and assign some default value in case its empty
$monto = (!empty($_POST['amount'])) ? $_POST['amount'] : 0; //0 is default value
$preference_data = array(
    "items" => array(
       array(
           "title" => "item",
           "quantity" => 1,
           "currency_id" => "usd",
           "unit_price" => $monto
       )
    )
);

答案 1 :(得分:0)

<?php
$preference_data = array(
    "items" => array(
        array(
            "title" => "RosquinhaPvP - NickDoPlayer - VIP",
            "quantity" => 1,
            "currency_id" => "BRL",
            "unit_price" => doubleval($monto)
        )
    )
);