“如何解决问题'number_format错误

时间:2019-06-04 07:45:09

标签: php

我想缩短结果。我使用了nummber_format,但始终会出现错误。有人可以帮我吗。

$arr = array();
    foreach ($order->orderPositions as $tax) {
        $arr[] = $tax->tax;
    }
    $unique_data = array_unique($arr);
    foreach ($unique_data as $val) {
        $totalTaxes[$val] = $order->orderPositions->where('tax', 
      $val)->sum('TotalPriceWithTax');
    }


  /*help is needed here*/  number_format((float)$unique_data,2);

1 个答案:

答案 0 :(得分:2)

环绕数组,并将其保存为新格式,无论是在新数组中还是在同一数组中

$unique_data = array_unique($arr);
foreach ($unique_data as &$val) { //notice the & if you want to change the data points in the unique array
    $totalTaxes[$val] = $order->orderPositions->where('tax', $val)->sum('TotalPriceWithTax');
    $val = number_format($val,2); // replaces the data in unique array
    $new[] = number_format($val,2); // add to new array if you need unique array

}