简体   繁体   English

如何使用PHP在foreach中回显数字格式分组数组?

[英]How to echo number format grouping array inside foreach using PHP ?

My question is referring to my previous question in this link How to echo specific multiple same records only once using foreach loops in PHP? 我的问题是指此链接中的上一个问题。 如何使用PHP中的foreach循环仅回显特定的多个相同记录一次? .

This is my current code 这是我当前的代码

Code : 代码:

$sqlTXT  = "SELECT * FROM TABLE";
$arr_old = DB::getInstance()->FetchArray($sqlTXT);

if(count($arr_old) > 0)
{
        $arr = array();

        foreach($arr_old as $key => $item)
    {
        if(!array_key_exists($item['ACCOUNT_NUMBER'], $arr))
        {
            $arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['ACCOUNT']   = $item['ACCOUNT'];
            $arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY']  = $item['CATEGORY'];
            $arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE']     = $item['VALUE'];
            $arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['FUND']      = $item['FUND'];
            $arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['AMOUNT']    = $item['AMOUNT'];
        }
        else
        {
            $arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['CATEGORY']   .= ",".$item['CATEGORY'];
            $arr[$item['ACCOUNT_NUMBER']][$item['ACCOUNT_NUMBER']]['VALUE']      .= ",".$item['VALUE'];
        }
    }

    ksort($arr, SORT_NUMERIC);

    echo "<table>";
    echo "<tr>";
    echo "<td>ACCOUNT</td>";
    echo "<td>CATEGORY</td>";
    echo "<td>VALUE</td>";
    echo "<td>FUND</td>";
    echo "<td>AMOUNT</td>";
    echo "</tr>";
    foreach($arr as $key => $item)
    {

        // Display Category
        $xpl = explode(",",$item[$key]['CATEGORY']);
        $n_category = "";
        foreach($xpl as $b => $a){
            $n_category .= ($b!=0) ? "<br>".$a : $a ;
        }

        // Display Value
        $trl = explode(",",$item[$key]['VALUE']);
        $n_value = "";
        foreach($trl as $c => $d){
            $n_value .= ($c!=0) ? "<br>".$d : $d ;

            // $new = number_format($n_value, 2, '.', ',');
        }

        echo "<tr>";
        echo "<td>".$item[$key]['ACCOUNT']."</td>";
        echo "<td>".$n_category."</td>";
        echo "<td>".$new."</td>";
        echo "<td>".$item[$key]['FUND']."</td>";
        echo "<td>".$item[$key]['AMOUNT']."</td>";
        echo "</tr>";
    }
    echo "</table>";

}

And the output as shown as below. 并输出如下图所示。

Output : 输出:

ACCOUNT         CATEGORY            VALUE           FUND            AMOUNT 

0001            Category1           10000           BIN         300,000.00
                Category2               0 
                Category3             500
                Category4           15000


0002            Category1            8500           BIN          70,000.00
                Category2            7000
                Category3             100
                Category4               0

But my current issue is I can't convert the VALUE column into 2 decimals format. 但是我当前的问题是我无法将VALUE列转换为2位小数格式。 When I added code 当我添加代码

$new = number_format($n_value, 2, '.', ',');

The output only shows the first value of array. 输出仅显示数组的第一个值。

I hope that someone can help me to solve this issue. 我希望有人可以帮助我解决这个问题。

Thanks in advance. 提前致谢。

You need to convert each value in the column independently. 您需要独立转换列中的每个值。 Try changing your foreach loop to this: 尝试将您的foreach循环更改为此:

foreach($trl as $c => $d){
    if (is_numeric($d)) $d = number_format($d, 2, '.', ',');
    $n_value .= ($c!=0) ? "<br>".$d : $d ;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM