在数组params中传递多个数据的正确方法:php

时间:2015-08-12 10:58:15

标签: php arrays

这是我的预言

foreach( $artist_price as $paypal_email => $price ) {
    // Calculation.
    $paypal_id_array[] = $paypal_email;

    $commission = ($commission_percent/100) * $price;
    $remaining_price = $price - $commission;
    $remaining_price_array[] = $remaining_price;
}

这就是我传递价值观的方式:

// Params.
$bodyParams = array(
    "receiverList.receiver(1).amount" => $remaining_price_array[0],
    "receiverList.receiver(2).email" => $paypal_id_array[1],
    "receiverList.receiver(2).amount" => $remaining_price_array[1],
    "receiverList.receiver(3).email" => $paypal_id_array[2],
    "receiverList.receiver(3).amount" => $remaining_price_array[2],
    "receiverList.receiver(4).email" => $paypal_id_array[3],
    "receiverList.receiver(4).amount" => $remaining_price_array[3],
    "receiverList.receiver(5).email" => $paypal_id_array[4],
    "receiverList.receiver(5).amount" => $remaining_price_array[4],
    "receiverList.receiver(6).email" => $paypal_id_array[5],
    "receiverList.receiver(6).amount" => $remaining_price_array[5],
    "receiverList.receiver(7).email" => $paypal_id_array[6],
    "receiverList.receiver(7).amount" => $remaining_price_array[6],
    "receiverList.receiver(8).email" => $paypal_id_array[7],
    "receiverList.receiver(8).amount" => $remaining_price_array[7]
);

是否有任何正确的方法来添加" receiverList.receiver(8).email"和" receiverList.receiver(8).amount"它在foreach循环中包含多少。例如:如果只有两个商家,那么只有两个商家。我正在做的方式是静态的,它也给出了未定义值的错误。虽然代码正在运行。

1 个答案:

答案 0 :(得分:1)

您可以在foreach循环中执行所有操作:

$bodyParams = array();
$count = 1;
foreach( $artist_price as $paypal_email => $price ) {
    // Calculation.
    $commission = ($commission_percent/100) * $price;
    $remaining_price = $price - $commission;

    $bodyParams["receiverList.receiver($count).email"] = $paypal_email;
    $bodyParams["receiverList.receiver($count).amount"] = $remaining_price;
    $count++;
}