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

标签 php

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

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

最佳答案

只要重新定义数组,您就应该走上成功之路。用户提供的代码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)

GST: 18 Amount: 25000 Discount: 10

Discount: 2500 Amount After Discount: 22500

GST: 4050

关于PHP array_map 多个参数不起作用 如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53895193/

相关文章:

php - docker nginx 502错误的网关

php 代码中的 javascript window.open

php - 动态多级菜单和子菜单php mysql

php - 如何让我的 php 代码更快?

php - 代码优化-mysql输出

php - 学说 DQL JOIN

php - 从我的 sql 表行中搜索至少一个逗号分隔值

php - 为什么我不能在PHP中调用此 protected 函数?

php - 在 PHPUnit 模拟对象中返回不同的结果

PHP ImagickException : not authorized