2

In my code below GST and Amount are printing properly but Discount is not printing the result. Why is Discount not printing? How can I solve this issue?

$GST = Array ( [0] => 18 );
$Amount = Array ( [0] => 25000 );
$Discount = Array ( [0] => 10 );
array_map( 
    function($GST, $Amount, $Discount){ 
        echo ' GST: '.$GST.' Amount: '.$Amount.' Discount: '.$Discount.'<br>';
        echo 'Discount: '.(($Amount * $Discount) / 100).' Amount After Discount: '.($Amount - (($Amount * $Discount) / 100)).'<br>';
        echo 'GST: '.(($Amount - (($Amount * $Discount) / 100)) * $GST / 100).'<br>';
        //return ($Amount - (($Amount * $Discount) / 100)) * $GST / 100; 
    }, 
    !is_array($GST) ? [] : $GST, 
    !is_array($Amount) ? [] : $Amount, 
    !is_array($Discount) ? [] : $Discount 
)
2
  • Is that how you defined your arrays? That should throw parse errors. Btw, I tested your code and it works (after changing how you define the arrays). Here's a demo: 3v4l.org/SqgJO Commented Dec 22, 2018 at 11:29
  • $GST = Array ( [0] => 18 ); is not valid PHP Commented Dec 22, 2018 at 19:00

1 Answer 1

2

A matter of redefining the arrays and you should be on your way of success. Code provided by user Magnus Eriksson.

$GST      = Array (18);
$Amount   = Array (25000);
$Discount = Array (10);
array_map( 
    function($GST, $Amount, $Discount){ 
        var_dump($GST, $Amount, $Discount);
        echo PHP_EOL;
        echo 'GST: '.$GST.' Amount: '.$Amount.' Discount: '.$Discount.PHP_EOL;
        echo 'Discount: '.(($Amount * $Discount) / 100).' Amount After Discount: '.($Amount - (($Amount * $Discount) / 100)).PHP_EOL;
        echo 'GST: '.(($Amount - (($Amount * $Discount) / 100)) * $GST / 100).PHP_EOL;
        //return ($Amount - (($Amount * $Discount) / 100)) * $GST / 100; 
    }, 
    !is_array($GST) ? [] : $GST, 
    !is_array($Amount) ? [] : $Amount, 
    !is_array($Discount) ? [] : $Discount 
);

Output:

int(18)

int(25000)

int(10)

GST: 18 Amount: 25000 Discount: 10

Discount: 2500 Amount After Discount: 22500

GST: 4050

Sign up to request clarification or add additional context in comments.

3 Comments

You could at least give me a shout out if you're going to copy/paste my demo? :-p
When your comment was posted I wasn't finished yet pasting the code. As you see you are referenced;-) Also submitted an edit for the question itself to make it readable.
No worries. :-)

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.