PHP array_map的多个参数不起作用如何解决?

时间:2018-12-22 11:21:25

标签: php

在我下面的代码中, GST 金额 可以正确打印,但 折扣< / em> 未打印结果。为什么 折扣 不打印?我该如何解决这个问题?

$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 
)

1 个答案:

答案 0 :(得分:2)

关于重新定义阵列的问题,您应该走上成功之路。用户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 
);

输出:

  

int(18)

     

int(25000)

     

int(10)

     

消费税:18金额:25000折扣:10

     

折扣:2500折扣后金额:22500

     

消费税:4050