在数组中加倍值,if语句,PHP

时间:2014-12-02 11:06:47

标签: php arrays if-statement

PHP不是很好,并且一直在努力学习如何在数组中创建if语句但经过一些研究我明白这是不可能的,所以如果你想检查是否有价值,你会怎么做?数组大于,例如100,如果它然后将该值乘以0.75,如果它不仅仅保持数组中的值。

它在第二个子表数组中,我进行som计算,我希望能够检查该值,然后在必要时将其乘以...任何帮助将不胜感激!

function details($parameters, $result) {
    $table = array();

    $subtable = array();
    $table[] = array("title" => get_text("details_0") . getInformation($parameters),
                     "rows"  => &$subtable);
    $subtable[] = array("title"       => get_text("details_1_2"),
                        "value"       => round($result->pris) . get_text("details_price"),
                        "collapsible" => false,
                        "tooltip"     => array(
                        "title"     => get_text("details_1_2_title"),
                        "content"   => get_text("details_1_2_content")));
    $subtable[] = array("title"       => get_text("details_1_4"),
                        "value"       => (round(12 * $result->varde / $result->sin) + round($parameters["extra"] * 0.106)) * $parameters["regel"] . get_text("price"),
                        "collapsible" => false,
                        "tooltip"     => array(
                        "title"     => get_text("details_1_4_title"),
                        "content"   => get_text("details_1_4_content")));

    $subtable[] ... more subtablearrays

    return $table;
}

2 个答案:

答案 0 :(得分:1)

为什么不先进行计算?

像这样:

$result = (round(12 * $result->varde / $result->sin) + round($parameters["extra"] * 0.106)) * $parameters["regel"] . get_text("price");
if($result > 100) {
  $result = $result * 0.75;
}
...
$subtable[] = array("title"       => get_text("details_1_4"),
                    "value"       => $result,
                    "collapsible" => false,
                    "tooltip"     => array(
                    "title"     => get_text("details_1_4_title"),
                    "content"   => get_text("details_1_4_content")));

答案 1 :(得分:1)

使用返回结果值的ternary operator

"value" => $condition_evaluated_to_boolean ? $true_value : $false_value