【问题标题】:I should use for, while or foreach array multidimensional calculation?我应该使用 for、while 还是 foreach 数组多维计算?
【发布时间】:2013-10-05 07:51:51
【问题描述】:
$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 多个小时!

【问题讨论】:

标签: php arrays loops calculator


【解决方案1】:

使用此代码

<?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

【讨论】:

  • 嗯,它的工作! +1 现在我明白了,我需要用每月的数量来改进我的价格数组。当我们使用例如:$price_anualy_seven 时得到不好的价格需要是 $price_anualy_seven * 12。但是现在,我相信通过你的 foreachs 我可以学习,也许我可以做到这一点。谢谢你。这很有帮助。
【解决方案2】:

试试这个:

<?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 />');
   }
}

【讨论】:

  • 谢谢。也为你+1。这比我需要的还要多。每年 * 12 的问题我可以设置为 'mo_desc' 而不是 0.85 我将使用 10.2 (0.85 * 12)。天哪,有了这个,我看到我很菜鸟。只有 4 或 5 行,即可使用。我尝试了 50、60 行但不能。感谢你们。真的爱你!
  • 酷享受开发:)
猜你喜欢
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 1970-01-01
  • 2013-10-27
  • 2011-12-26
  • 2011-03-03
  • 2012-02-15
  • 2016-04-10
相关资源
最近更新 更多