Rushabh Shah
Rushabh Shah

Reputation: 133

PHP array_map multiple parameters not working How to resolve?

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 
)

Upvotes: 2

Views: 76

Answers (1)

ZF007
ZF007

Reputation: 3731

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

Upvotes: 2

Related Questions