0

Not very good with PHP and have been struggling with how you can make an if statement inside an array but after some research I understand that it's not possible to do, so how would you do if you want to check if the value of the array is bigger than, for example 100 and if it is then multiply that value with 0.75 and if it's not just keep the value as it is in the array.

It's in the second subtable array where I make som calculations that I want to be able to check the value and then if necessary multiply it... Any help would be greatly appreciated!

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;
}
1
  • if ($value > X) $value = Y; Commented Dec 2, 2014 at 11:10

2 Answers 2

1

Why don't you make the calculation before?

Like this:

$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")));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the obvious answer I couldn't think of :)
1

Use a ternary operator which returns the resulting value

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.