簡體   English   中英

PHP 未顯示循環中數組中的所有數據

[英]PHP not showing all data in array from loop

將數據輸出到數組

public function GetProfitByMonth($d1, $d2){
    $sql=odbc_exec($this->connections, "select con.ID, con.ShortName as NameAK,
                            ROUND(ISNULL(SUM(tik.TotalSub)-SUM(tik.TotalAG), 0), 2) as Profit,
                            MONTH(tik.DEALDATE) as month_num
                            from Counteragent as con
                            left join Tickets as tik on tik.AgentID=con.ID
                            where con.ValueType in (2,3) and con.Active='1' and (DEALDATE between '".$d1."' and '".$d2."')
                            group by con.ID, con.ShortName, MONTH(tik.DEALDATE)
                            order by con.ShortName, MONTH(tik.DEALDATE) ;");
    $tblResult=array();
    while ($row = odbc_fetch_array($sql)) {
        $tblResult[]=$row;
    }
    odbc_free_result($sql);
   

Array
(
    [0] => Array
        (
            [Profit] => 11218.30
            [month_num] => 8
        )    
    [1] => Array
        (
            [Profit] => 1152.15
            [month_num] => 8
        )

    ....

    [4] => Array
        (
            [Profit] => 119837.81
            [month_num] => 8
        )    
)

我只需要顯示數組中的month_num => Profi

這樣做

$tblResult =  array();
        while ($row = odbc_fetch_array($sql)) {
            $tblResult[$row['mon_num']] = $row['Profit'];
        }
        odbc_free_result($sql);
        return $tblResult;

但結果在數組中只顯示最后一個值:

Array
(
    [8] => 119837.81
)

如何使數組顯示所有數據?

這是因為對於原始數組中的所有條目, month_num = 8
您的 while 循環前兩次迭代有效地評估為:

$tblResult[8] = 11218.30
$tblResult[8] = 1152.15

要以您想要的形式獲得結果,您需要month_num是唯一的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM