0
$price = array(
    'value' => 40,
    'mo_desc' => array(
        'monthly' => 1,
        'quartely' => 0.95,
        'semi-anualy' => 0.90,
        'anualy' => 0.85,
        'bi-anualy' => 0.75
    ),
    'plan_desc' => array(
        'one' => 1,
        'two' => 1.9,
        'tree' => 3.6,
        'four' => 6.8,
        'five' => 9.6,
        'six' => 12,
        'seven' => 16.8,
        'eight' => 20.8
    )
);

我需要得到变量组合。

$price_monthly_plan_one = $price['value'] * $price['mo_desc']['monthly'] * $price['plan_desc']['one'];
$price_quartely_plan_one = $price['value'] * $price['mo_desc']['quartely'] * $price['plan_desc']['one'];
$price_quartely_plan_two = $price['value'] * $price['mo_desc']['quartely'] * $price['plan_desc']['two'];

...我用 for、while 和 foreach 做了很多尝试。我不能那样做。如果有人可以帮忙!

我正在尝试执行一个函数,我将发送 $price 并且该函数将返回如下价格:

$price = array(
    'plan_1' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx),
    'plan_2' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx),
    'plan_3' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx),
    'plan_4' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx),
    'plan_5' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx),
    'plan_6' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx),
    'plan_7' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx),
    'plan_8' => array ('price_monthly' => 40,'price_quartely' => xx,'price_semi-anualy' => xx,'price_anualy' => xx,'price_bianualy' => xx)
);

难……非常难……尝试了8个多小时!

4

2 回答 2

0

尝试这个:

<?php
$price = array(
    'value' => 40,
    'mo_desc' => array(
        'monthly' => 1,
        'quartely' => 0.95,
        'semi-anualy' => 0.90,
        'anualy' => 0.85,
        'bi-anualy' => 0.75
    ),
    'plan_desc' => array(
        'one' => 1,
        'two' => 1.9,
        'tree' => 3.6,
        'four' => 6.8,
        'five' => 9.6,
        'six' => 12,
        'seven' => 16.8,
        'eight' => 20.8
    )

);

$value = $price['value'];
$mo_desc = $price['mo_desc'];
$plan_desc = $price['plan_desc'];

foreach($mo_desc as $key=> $month) {

   echo '<h2>'.$key.'</h2>';

   foreach($plan_desc as $key1=> $plan) {
        echo '<br />Price '.$key. ' plan '.$key1.': ';
        $price = $value * $month * $plan;
        echo $price;
        echo ('<br />');
   }
}
于 2013-10-05T08:18:08.727 回答
0

使用此代码

<?php

$price = array(
    'value' => 40,
    'mo_desc' => array(
        'monthly' => 1,
        'quartely' => 0.95,
        'semi-anualy' => 0.90,
        'anualy' => 0.85,
        'bi-anualy' => 0.75
    ),
    'plan_desc' => array(
        'one' => 1,
        'two' => 1.9,
        'tree' => 3.6,
        'four' => 6.8,
        'five' => 9.6,
        'six' => 12,
        'seven' => 16.8,
        'eight' => 20.8
    )
);


    foreach ($price['mo_desc'] as $mo_desc_key=>$mo_desc_val) {
        foreach ($price['plan_desc'] as $plan_desc_key=>$plan_desc_val) {
            $var_name   = 'price_'.$mo_desc_key.'_plan_'.$plan_desc_key;
            $$var_name = $price['value'] * $mo_desc_val * $plan_desc_val;
        }
    }
echo $price_monthly_plan_one."<br>";  // Output 40
echo $price_quartely_plan_one."<br>"; // Output 38
echo $price_quartely_plan_two."<br>"; // Output 72.2
于 2013-10-05T08:10:06.330 回答