考虑这个数组
array(6) {
["id"]=>
int(346058)
["amount"]=>
string(5) "60.00"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["negative_sum"]=>
float(-60)
["negative"]=>
array(11) {
[0]=>
array(5) {
["id"]=>
int(346060)
["amount"]=>
string(5) "-2.20"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[1]=>
array(5) {
["id"]=>
int(348152)
["amount"]=>
string(5) "-7.50"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[2]=>
array(5) {
["id"]=>
int(350163)
["amount"]=>
string(5) "-7.70"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[3]=>
array(5) {
["id"]=>
int(351996)
["amount"]=>
string(5) "-5.20"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[4]=>
array(5) {
["id"]=>
int(353919)
["amount"]=>
string(5) "-5.00"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[5]=>
array(5) {
["id"]=>
int(354768)
["amount"]=>
string(5) "-2.50"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[6]=>
array(5) {
["id"]=>
int(356650)
["amount"]=>
string(5) "-5.00"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[7]=>
array(5) {
["id"]=>
int(361683)
["amount"]=>
string(5) "-5.00"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[8]=>
array(5) {
["id"]=>
int(361836)
["amount"]=>
string(5) "-7.50"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[9]=>
array(5) {
["id"]=>
int(364145)
["amount"]=>
string(5) "-7.90"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
[10]=>
array(5) {
["id"]=>
int(364426)
["amount"]=>
string(5) "-4.50"
["id_shop_where_transaction_is_done"]=>
int(300)
["id_shop_where_money_come_from"]=>
NULL
["id_referring_transaction"]=>
int(346058)
}
}
}一点解释:
$array[0]['negative_sum']是$array[0]['negative'][$i]['amount']的总和
(如果你这样做,总数是-60。)
现在,我需要检查$ array [0] ['amount']是否为negative_sum,所以有些东西。
$positive_amount = $transaction[$i]['amount'];
$negative_amount = $transaction[$i]['negative_sum'];
if ($positive_amount>abs($negative_amount)) {
$difference = -$positive_amount-$negative_amount;
var_dump($difference);
$data['difference'] = $difference;
}首先,特定情况下的if代码不会被执行,因为60不会大于abs(-60)。但它被执行。
第二个,var_dump($差异)输出是
float(-7,105427357601E-15)不明白为什么。谢谢。
Per @OptimusCrime答案。我修改了一下代码。
$positive_amount = $transaction[$i]['amount'];
$negative_amount = $transaction[$i]['negative_sum'];
var_dump(number_format($positive_amount, 90));
var_dump(number_format($negative_amount, 90));
if ($positive_amount>abs($negative_amount)) {
$difference = -$positive_amount-$negative_amount;
var_dump($difference);
$data['difference'] = $difference;
}该var_dump输出两个:
string(93) "60.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
string(94) "-60.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"