按数组中的指定列对行进行分组

Posted

技术标签:

【中文标题】按数组中的指定列对行进行分组【英文标题】:Grouping row by specified column from array 【发布时间】:2017-08-17 02:08:56 【问题描述】:

我有这个数组

   Array
(
    [0] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.4
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150400005
            [ItemName] => BIG BAG
            [OriginalPrice] => 289900.00
            [Price] => 289900.00
            [QtyBegining] => 0
            [QtyIn] => 4
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 4
            [QtyOpname] => 0
            [QtyAdjust] => -4
            [TotalAdjust] => -1159600.00
            [rak] => 
            [line] => 
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

    [1] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.5
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150500063
            [ItemName] => BIG BAG
            [OriginalPrice] => 479900.00
            [Price] => 479900.00
            [QtyBegining] => 0
            [QtyIn] => 0
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 0
            [QtyOpname] => 1
            [QtyAdjust] => 0
            [TotalAdjust] => .00
            [rak] => SS
            [line] => SS
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

    [2] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.5
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150500063
            [ItemName] => BIG BAG
            [OriginalPrice] => 479900.00
            [Price] => 479900.00
            [QtyBegining] => 0
            [QtyIn] => 1
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 1
            [QtyOpname] => 0
            [QtyAdjust] => 0
            [TotalAdjust] => .00
            [rak] => 
            [line] => 
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

    [3] => stdClass Object
        (
            [TransactionNo] => OPM-EC-K-LFMCD2-21.6
            [StokOpnameTransNo] => OPM-EC-K-LFMCD2-21
            [ItemCode] => BBG150500081
            [ItemName] => BIG BAG
            [OriginalPrice] => 229900.00
            [Price] => 229900.00
            [QtyBegining] => 0
            [QtyIn] => 0
            [QtySales] => 0
            [QtyMutasi] => 0
            [QtyStock] => 0
            [QtyOpname] => 1
            [QtyAdjust] => 0
            [TotalAdjust] => .00
            [rak] => SS
            [line] => SS
            [TransDate] => 2017-03-31 10:46:00.000
            [CustomerCode] => K-LFMCD2
            [CreatedBy] => 1208496.
            [Remark] => re
            [Remark2] => re
            [SOTerakhir] => 
            [TglSoTerakhir] => 
        )

)

我的数组来自查询结果。然后我试图把它放到 html 表中,所以我尝试这种方式。

<table class="table ">
                    <thead>
                        <tr>
                            <th>No</th>
                            <th>ItemCode</th>
                            <th>ItemName</th>
                            <th>TransNo</th>
                            <th>Original Price</th>
                            <th>Price</th>
                             <th>QtyBeginning</th>
                            <th>QtyIn</th>
                            <th>QtySales</th>
                            <th>QtyMutasi</th>
                            <th>QtyStock</th>
                            <th>QtyOpname</th>
                            <th>QtyAdjust</th>
                            <th>TotalAdjust</th>
                            <th>Rak</th>
                            <th>Line</th>
                        </tr>
                    </thead>
                    <tbody> <?php $x=0; foreach ($datadetail as $data)  $x++;?>
                                 <tr>
                                    <td><?=$x;?></td>
                                    <td><?=$data->ItemCode;?></td>
                                    <td><?=$data->ItemName;?></td>
                                    <td><?=$data->StokOpnameTransNo;?></td>
                                    <td><?=$data->OriginalPrice;?></td>
                                    <td><?=$data->Price;?></td>
                                    <td><?=$data->QtyBegining;?></td>
                                    <td><?=$data->QtyIn;?></td>
                                    <td><?=$data->QtySales;?></td>
                                    <td><?=$data->QtyMutasi;?></td>
                                    <td><?=$data->QtyStock;?></td>
                                    <td><?=$data->QtyOpname;?></td>
                                    <td><?=$data->QtyAdjust;?></td>
                                    <td><?=currency($data->TotalAdjust);?></td>
                                    <td><?=$data->rak;?></td>
                                    <td><?=$data->line;?></td>
                                </tr>
                            <?php  ?> 

                    </tbody>

            </table>

结果只是一个像这样的简单表格。显示我的表时没有问题。但是,我想将结果(在 HTML 中)更改为这样的内容

就像按ItemCode 分组一样。所以在每个项目代码之后我想总结QtyBeginning,QtyIn,QtyOut etc

【问题讨论】:

【参考方案1】:

我不知道你的表结构,但我认为你应该能够从你的数据库中组织你的数据

无论哪种方式,我都会尝试向您展示如何做到这一点(完整的测试脚本)

测试数据:

$arrTestData = [];

$obj = new stdClass();
$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.4";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150400005";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "289900.00";
$obj->Price = "289900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "4";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "4";
$obj->QtyOpname = "2";
$obj->QtyAdjust = "-4";
$obj->TotalAdjust = "-1159600.00";
$obj->rak = "";
$obj->line = "";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();
$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.4";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150400005";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "289900.00";
$obj->Price = "289900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "2";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "4";
$obj->QtyOpname = "4";
$obj->QtyAdjust = "-4";
$obj->TotalAdjust = "-1159600.00";
$obj->rak = "";
$obj->line = "";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();

$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.5";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150500063";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "479900.00";
$obj->Price = "479900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "0";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "0";
$obj->QtyOpname = "1";
$obj->QtyAdjust = "0";
$obj->TotalAdjust = ".00";
$obj->rak = "SS";
$obj->line = "SS";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();

$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.5";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150500063";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "479900.00";
$obj->Price = "479900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "1";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "1";
$obj->QtyOpname = "0";
$obj->QtyAdjust = "0";
$obj->TotalAdjust = ".00";
$obj->rak = "";
$obj->line = "";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

$obj = new stdClass();

$obj->TransactionNo = "OPM-EC-K-LFMCD2-21.6";
$obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
$obj->ItemCode = "BBG150500081";
$obj->ItemName = "BIG BAG";
$obj->OriginalPrice = "229900.00";
$obj->Price = "229900.00";
$obj->QtyBegining = "0";
$obj->QtyIn = "0";
$obj->QtySales = "0";
$obj->QtyMutasi = "0";
$obj->QtyStock = "0";
$obj->QtyOpname = "1";
$obj->QtyAdjust = "0";
$obj->TotalAdjust = ".00";
$obj->rak = "SS";
$obj->line = "SS";
$obj->TransDate = "2017-03-31 10:46:00.000";
$obj->CustomerCode = "K-LFMCD2";
$obj->CreatedBy = "1208496.";
$obj->Remark = "re";
$obj->Remark2 = "re";
$obj->SOTerakhir = "";
$obj->TglSoTerakhir = "";
$arrTestData[] = $obj;

您需要一个项目集合或类似的东西来保存和结构化您的数据

class ItemCollection extends ArrayObject


    private $strCollectionCode;
    private $strCollectionName;

    public function addItem($objItem)
    
        $this->strCollectionCode = $objItem->ItemCode;
        $this->strCollectionName = $objItem->ItemName;
        $this->append($objItem);
    

    public function get($key)
    
        return $this->$key;
    

    public function getSummaries($strKey)
    
        $floatNumber = 0;
        foreach($this AS $objItem)
        
            $floatNumber += floatval($objItem->$strKey);
        
        return $floatNumber;
    

包装器应该包含这个集合

class ItemCollectionWrapper

    private $arrCollections = array();

    public function __construct($arrData = false)
    
        if (is_array($arrData))
        
            $this->addItemsFromArray($arrData);
        
    

    public function addItem($objItem)
    
        if (isset($this->arrCollections[$objItem->ItemCode]))
        
            $objCollection = $this->arrCollections[$objItem->ItemCode];
        
        else
        
            $objCollection = new ItemCollection();
            $this->arrCollections[$objItem->ItemCode] = $objCollection;
        

        $objCollection->addItem($objItem);
    

    public function addItemsFromArray($arrData)
    
        foreach($arrData AS $obj)
        
            $this->addItem($obj);
        
    

    public function getCollections()
    
        return $this->arrCollections;
    

最后是我们的观点

<?php
    $obj = new ItemCollectionWrapper($arrTestData);

?>
<!DOCTYPE html> 
<html lang=en> 
<head> 
    <meta charset=utf-8> 
    <meta content="IE=edge" http-equiv=X-UA-Compatible> 
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">

    <!-- Latest compiled and minified javascript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
<style type="text/css">
    td.summaries 
    
        background:#c6c600;
    
</style>
<table class="table">
<thead>
    <tr>
        <th>No</th>
        <th>ItemCode</th>
        <th>ItemName</th>
        <th>TransNo</th>
        <th>Original Price</th>
        <th>Price</th>
         <th>QtyBeginning</th>
        <th>QtyIn</th>
        <th>QtySales</th>
        <th>QtyMutasi</th>
        <th>QtyStock</th>
        <th>QtyOpname</th>
        <th>QtyAdjust</th>
        <th>TotalAdjust</th>
        <th>Rak</th>
        <th>Line</th>
    </tr>
</thead>
<tbody>
<?php
foreach($obj->getCollections() AS $objCollection)

    $number = 1;
?>
    <?php
    foreach($objCollection AS $key => $objItem) :
    ?>
     <tr>
        <?php
        if ($key == 0)  :
        ?>
        <td rowspan="<?=$objCollection->count();?>"><?=$number;?></td>
        <td rowspan="<?=$objCollection->count();?>"><?=$objCollection->get("strCollectionCode");?></td>
        <td rowspan="<?=$objCollection->count();?>"><?=$objCollection->get("strCollectionName");?></td>
        <?php endif; ?>
        <td><?=$objItem->StokOpnameTransNo;?></td>
        <td><?=$objItem->OriginalPrice;?></td>
        <td><?=$objItem->Price;?></td>
        <td><?=$objItem->QtyBegining;?></td>
        <td><?=$objItem->QtyIn;?></td>
        <td><?=$objItem->QtySales;?></td>
        <td><?=$objItem->QtyMutasi;?></td>
        <td><?=$objItem->QtyStock;?></td>
        <td><?=$objItem->QtyOpname;?></td>
        <td><?=$objItem->QtyAdjust;?></td>
        <td><?=($objItem->TotalAdjust);?></td>
        <td><?=$objItem->rak;?></td>
        <td><?=$objItem->line;?></td>
    </tr>
    <?php
    endforeach;
    ?>
    <tr>
        <td colspan="6"></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyBegining");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyIn");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtySales");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyMutasi");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyStock");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyOpname");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("QtyAdjust");?></td>
        <td class="summaries"><?=$objCollection->getSummaries("TotalAdjust");?></td>
        <td colspan="2"></td>
    </tr>
<?php
    $number++;

?>
</tbody>
</table>
</body>
</html>

我希望它有所帮助——如果你研究了这段代码——你应该能够以任何你喜欢的方式扩展它。

顺便说一句:您可以将 ItemCollectionWrapper 和 ItemCollection 类放在模型代码下方 如果你不知道怎么做 - 给我看看你的模型和控制器,我会告诉你的。

【讨论】:

【参考方案2】:

首先我努力提高我的英语水平。

第二:我有这个问题的解决方案,试试这个希望对你有所帮助。 想法 -> 我尝试比较 SQL 数组返回的项目代码(你 mutch 排序 Itemcode 按 asc 顺序。

然后试试这段代码 php代码

<?php 
class Item
    protected $data;

    public function _construct($itemData)
        $_arr = array(
            'QtyBegining'   => $itemData->QtyBegining,
            'QtyIn'         => $itemData->QtyIn,
            'QtySales'      => $itemData->QtySales,
            'QtyStock'      => $itemData->QtyStock,
            'QtyOpname'     => $itemData->QtyOpname,
            'QtyAdjust'     => $itemData->QtyAdjust,
            'TotalAdjust'   => $itemData->TotalAdjust
        )
        $this->data = $_arr;
    

    public function setData($key, $value)
        $this->data[$key] = $value;
    

    public function getData($key)
        return $this->data[$key];
    


    public $QtyBegining;
    public $QtyIn;
    public $QtySales;
    public $QtyStock;
    public $QtyOpname;
    public $QtyAdjust;
    public $TotalAdjust;

?>

<tbody>
    <?php $x=0;
    $revertItemcode = null; 
    $revertSum = null;
    foreach ($datadetail as $data) 
     
        $hasNewItemcode = false;
        if($revertItemcode == null) 

            //If first Item
            $revertItemcode = $data->ItemCode;

            //Set revert itemcode to new Itemcode
            $revertSum = new Item($data);
        else
            //If have not equa with revert item code
            if($revertItemcode != $data->ItemCode;)
                $hasNewItemcode = true;
            
        

        //Sum with revert item if equa Itemcode
        if(!$hasNewItemcode)

            $revertSum->setData('QtyBegining', $revertSum->getData('QtyBegining') + $data->QtyBegining);

            $revertSum->setData('QtyIn', $revertSum->getData('QtyIn') + $data->QtyIn);

            $revertSum->setData('QtySales', $revertSum->getData('QtySales') + $data->QtySales);

            $revertSum->setData('QtyStock', $revertSum->getData('QtyStock') + $data->QtyStock);

            $revertSum->setData('QtyOpname', $revertSum->getData('QtyOpname') + $data->QtyOpname);

            $revertSum->setData('QtyAdjust', $revertSum->getData('QtyAdjust') + $data->QtyAdjust);

            $revertSum->setData('TotalAdjust', $revertSum->getData('revertSum') + $data->TotalAdjust);            
        
        $x++;
    ?>  
        <?php
        //Echo sumary row with revert item code. 
        if($hasNewItemcode):?>
        <tr class="sumary-item-code">
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td><?=$revertSum->getData('QtyBegining');?></td>
            <td><?=$revertSum->getData('QtyIn');?></td>
            <td><?=$revertSum->getData('QtySales');?></td>
            <td><?=$revertSum->getData('QtyMutasi');?></td>
            <td><?=$revertSum->getData('QtyStock');?></td>
            <td><?=$revertSum->getData('QtyOpname');?></td>
            <td><?=$revertSum->getData('QtyAdjust');?></td>
            <td><?=currency($revertSum->getData('TotalAdjust'));?></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
        </tr>
        <?php 
        //Set revert itemcode to new Itemcode
        $revertSum = new Item($data);
        ?>
        <?php endif;?>
        <tr>
            <td><?=$x;?></td>
            <td><?=$data->ItemCode;?></td>
            <td><?=$data->ItemName;?></td>
            <td><?=$data->StokOpnameTransNo;?></td>
            <td><?=$data->OriginalPrice;?></td>
            <td><?=$data->Price;?></td>
            <td><?=$data->QtyBegining;?></td>
            <td><?=$data->QtyIn;?></td>
            <td><?=$data->QtySales;?></td>
            <td><?=$data->QtyMutasi;?></td>
            <td><?=$data->QtyStock;?></td>
            <td><?=$data->QtyOpname;?></td>
            <td><?=$data->QtyAdjust;?></td>
            <td><?=currency($data->TotalAdjust);?></td>
            <td><?=$data->rak;?></td>
            <td><?=$data->line;?></td>
        </tr>
    <?php  ?> 

</tbody>

【讨论】:

对不起,我有点困惑。我正在使用代码点火器。我应该把Class Item放在哪里? 任何地方都可以,我不太了解 Codeigniter,但我认为你可以从模型或助手中定义类【参考方案3】:

@YVS1102 首先,您需要安排数据(关联数组),以便您可以根据需要进行循环。

foreach($datadetail  as $k=>$v)
        $result[$v->ItemCode][] = $v;
    

现在你可以循环如下

$i=0;
    foreach($result as $key=>$value)
        $i++; 
        $j= $qtyBegining = $qtyIn = $qtySale = $qtyMutasi = $qtyStock = $qtyOpname = $qtAdjust = $totalAdjust = 0;
        /** we can use foreach also however we need to calculate the Qty so we will use for **/           

        for($k=0;$k<=(count($value));$k++ )
             <tr>
                <?php if($j==0)?>
                   <td rowspan="<?php echo count($value)+1;?>"><?php echo $i;?></td>    
                   <td rowspan="<?php echo count($value)+1;?>"><?php echo $value[$k]->ItemCode;?></td>    
                   <td rowspan="<?php echo count($value)+1;?>"><?php echo $value[$k]->ItemName;?></td>    
                <?php ?>  
                   <?php if(isset($value[$k]) && !empty($value[$k]))
                      $qtyBegining = $qtyBegining + $value[$k]->QtyBegining;
                      $qtyIn = $qtyIn + $value[$k]->QtyIn;
                      $qtySale = $qtySale + $value[$k]->QtySales;
                      $qtyMutasi = $qtyMutasi + $value[$k]->QtyMutasi;
                      $qtyStock = $qtyStock + $value[$k]->QtyStock;
                      $qtyOpname = $qtyOpname + $value[$k]->QtyOpname;
                      $qtAdjust = $qtAdjust + $value[$k]->QtyAdjust;
                      $totalAdjust = $totalAdjust + $value[$k]->TotalAdjust;
                   ?>
                   <td><?php echo ($k == count($value) ?  ' ' : $value[$k]->StokOpnameTransNo ;?></td> 
                   <td><?php echo ($k == count($value) ?  ' ' : $value[$k]->OriginalPrice ;?></td> 
                   <td><?php echo ($k == count($value) ?  ' ' : $value[$k]->Price ;?></td> 
                   <td><?php echo ($k == count($value) ?  $qtyBegining : $value[$k]->QtyBegining ;?></td> 
                   <td><?php echo ($k == count($value) ?  $qtyIn : $value[$k]->QtyIn ;?></td> 
                   <td><?php echo ($k == count($value) ?  $qtySale : $value[$k]->QtySales ;?></td> 
                   <td><?php echo ($k == count($value) ?  $qtyMutasi : $value[$k]->QtyMutasi ;?></td> 
                   <td><?php echo ($k == count($value) ?  $qtyStock : $value[$k]->QtyStock ;?></td> 
                   <td><?php echo ($k == count($value) ?  $qtyOpname : $value[$k]->QtyOpname ;?></td> 
                   <td><?php echo ($k == count($value) ?  $qtAdjust : $value[$k]->QtyAdjust ;?></td> 
                   <td><?php echo ($k == count($value) ?  $totalAdjust : currency($value[$k]->TotalAdjust) ;?></td> 
                   <td><?php echo ($k == count($value) ?  ' ' : $value[$k]->rak ;?></td> 
                   <td><?php echo ($k == count($value) ?  ' ' : $value[$k]->line ;?></td> 

            </tr>
            $j++;
           
        

您不需要在上述方法中创建新类。

【讨论】:

是的,我同意你不需要创建一个新类,但有了一个保存数据的类,你的代码就更简洁了。对我来说,这可行,但看起来“丑陋”。 据我了解,@YVS1102 正在通过 codeigniter 活动记录从数据库中获取数据(数据已经是对象格式)。我们只是将它保存到数组中并将数组循环到视图中。然而,在新的类方法中,我们试图将相同的数据再次分配给另一个类对象。 是的,没错。但是在新类中使用它可以让您轻松地为数字提供格式,并轻松设置空值返回。并且您在所有应用程序中保持统一的数据格式,以防万一您想在其他视图中再次显示(您不必编写和记住每行使用的所有函数)。【参考方案4】:

示例数据:感谢@sintakonte 提供测试数据

    $arrTestData = [];

    $obj = new stdClass();
    $obj->TransactionNo = "OPM-EC-K-LFMCD2-21.4";
    $obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
    $obj->ItemCode = "BBG150400005";
    $obj->ItemName = "BIG BAG";
    $obj->OriginalPrice = "289900.00";
    $obj->Price = "289900.00";
    $obj->QtyBegining = "0";
    $obj->QtyIn = "4";
    $obj->QtySales = "0";
    $obj->QtyMutasi = "0";
    $obj->QtyStock = "4";
    $obj->QtyOpname = "2";
    $obj->QtyAdjust = "-4";
    $obj->TotalAdjust = "-1159600.00";
    $obj->rak = "";
    $obj->line = "";
    $obj->TransDate = "2017-03-31 10:46:00.000";
    $obj->CustomerCode = "K-LFMCD2";
    $obj->CreatedBy = "1208496.";
    $obj->Remark = "re";
    $obj->Remark2 = "re";
    $obj->SOTerakhir = "";
    $obj->TglSoTerakhir = "";
    $arrTestData[] = $obj;

    $obj = new stdClass();
    $obj->TransactionNo = "OPM-EC-K-LFMCD2-21.4";
    $obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
    $obj->ItemCode = "BBG150400005";
    $obj->ItemName = "BIG BAG";
    $obj->OriginalPrice = "289900.00";
    $obj->Price = "289900.00";
    $obj->QtyBegining = "0";
    $obj->QtyIn = "2";
    $obj->QtySales = "0";
    $obj->QtyMutasi = "0";
    $obj->QtyStock = "4";
    $obj->QtyOpname = "4";
    $obj->QtyAdjust = "-4";
    $obj->TotalAdjust = "-1159600.00";
    $obj->rak = "";
    $obj->line = "";
    $obj->TransDate = "2017-03-31 10:46:00.000";
    $obj->CustomerCode = "K-LFMCD2";
    $obj->CreatedBy = "1208496.";
    $obj->Remark = "re";
    $obj->Remark2 = "re";
    $obj->SOTerakhir = "";
    $obj->TglSoTerakhir = "";
    $arrTestData[] = $obj;

    $obj = new stdClass();

    $obj->TransactionNo = "OPM-EC-K-LFMCD2-21.5";
    $obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
    $obj->ItemCode = "BBG150500063";
    $obj->ItemName = "BIG BAG";
    $obj->OriginalPrice = "479900.00";
    $obj->Price = "479900.00";
    $obj->QtyBegining = "0";
    $obj->QtyIn = "0";
    $obj->QtySales = "0";
    $obj->QtyMutasi = "0";
    $obj->QtyStock = "0";
    $obj->QtyOpname = "1";
    $obj->QtyAdjust = "0";
    $obj->TotalAdjust = ".00";
    $obj->rak = "SS";
    $obj->line = "SS";
    $obj->TransDate = "2017-03-31 10:46:00.000";
    $obj->CustomerCode = "K-LFMCD2";
    $obj->CreatedBy = "1208496.";
    $obj->Remark = "re";
    $obj->Remark2 = "re";
    $obj->SOTerakhir = "";
    $obj->TglSoTerakhir = "";
    $arrTestData[] = $obj;

    $obj = new stdClass();

    $obj->TransactionNo = "OPM-EC-K-LFMCD2-21.5";
    $obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
    $obj->ItemCode = "BBG150500063";
    $obj->ItemName = "BIG BAG";
    $obj->OriginalPrice = "479900.00";
    $obj->Price = "479900.00";
    $obj->QtyBegining = "0";
    $obj->QtyIn = "1";
    $obj->QtySales = "0";
    $obj->QtyMutasi = "0";
    $obj->QtyStock = "1";
    $obj->QtyOpname = "0";
    $obj->QtyAdjust = "0";
    $obj->TotalAdjust = ".00";
    $obj->rak = "";
    $obj->line = "";
    $obj->TransDate = "2017-03-31 10:46:00.000";
    $obj->CustomerCode = "K-LFMCD2";
    $obj->CreatedBy = "1208496.";
    $obj->Remark = "re";
    $obj->Remark2 = "re";
    $obj->SOTerakhir = "";
    $obj->TglSoTerakhir = "";
    $arrTestData[] = $obj;

    $obj = new stdClass();

    $obj->TransactionNo = "OPM-EC-K-LFMCD2-21.6";
    $obj->StokOpnameTransNo = "OPM-EC-K-LFMCD2-21";
    $obj->ItemCode = "BBG150500081";
    $obj->ItemName = "BIG BAG";
    $obj->OriginalPrice = "229900.00";
    $obj->Price = "229900.00";
    $obj->QtyBegining = "0";
    $obj->QtyIn = "0";
    $obj->QtySales = "0";
    $obj->QtyMutasi = "0";
    $obj->QtyStock = "0";
    $obj->QtyOpname = "1";
    $obj->QtyAdjust = "0";
    $obj->TotalAdjust = ".00";
    $obj->rak = "SS";
    $obj->line = "SS";
    $obj->TransDate = "2017-03-31 10:46:00.000";
    $obj->CustomerCode = "K-LFMCD2";
    $obj->CreatedBy = "1208496.";
    $obj->Remark = "re";
    $obj->Remark2 = "re";
    $obj->SOTerakhir = "";
    $obj->TglSoTerakhir = "";
    $arrTestData[] = $obj;

现在,创建另一个键为 itemcode 的数组,或者我可以说按 itemcode 过滤数据组

    $newArr = array();
    foreach($arrTestData as $data)
        $newArr[$data->ItemCode][] = $data;
    

现在,显示结果数据:

    echo '<table border="2">
                <tr>
                    <td>item code</td>
                    <td>item name</td>
                    <td>QtyBegining</td>
                    <td>QtyIn</td>
                    <td>QtySales</td>
                    <td>QtyMutasi</td>
                    <td>QtyStock</td>
                    <td>QtyOpname</td>
                    <td>QtyAdjust</td>
                    <td>TotalAdjust</td>
                </tr>';  //add others columns as required
    foreach ($newArr as $key => $item)
        $QtyBegining = $QtyIn = $QtySales = $QtyMutasi = $QtyStock = $QtyOpname = $QtyAdjust = $TotalAdjust = 0; // to store sum of each field itemcode wise
        for($i=0; $i<count($item); $i++)
            echo '<tr>'
                    . '<td>'.$item[$i]->ItemCode.'</td>'
                    . '<td>'.$item[$i]->ItemName.'</td>'
                    . '<td>'.$item[$i]->QtyBegining.'</td>'
                    . '<td>'.$item[$i]->QtyIn.'</td>'
                    . '<td>'.$item[$i]->QtySales.'</td>'
                    . '<td>'.$item[$i]->QtyMutasi.'</td>'
                    . '<td>'.$item[$i]->QtyStock.'</td>'
                    . '<td>'.$item[$i]->QtyOpname.'</td>'
                    . '<td>'.$item[$i]->QtyAdjust.'</td>'
                    . '<td>'.$item[$i]->TotalAdjust.'</td>'
                    . '</tr>';
            $QtyBegining += $item[$i]->QtyBegining;
            $QtyIn += $item[$i]->QtyIn;
            $QtySales += $item[$i]->QtySales;
            $QtyMutasi += $item[$i]->QtyMutasi;
            $QtyStock += $item[$i]->QtyStock;
            $QtyOpname += $item[$i]->QtyOpname;
            $QtyAdjust += $item[$i]->QtyAdjust;
            $TotalAdjust += $item[$i]->TotalAdjust;        
         // end for loop
        //now display the sum of each field, note: add empty td as many required
        echo '<tr>'
            . '<td>&nbsp;</td>'
                    . '<td>&nbsp;</td>'
                    . '<td>'.$QtyBegining.'</td>'
                    . '<td>'.$QtyIn.'</td>'
                    . '<td>'.$QtySales.'</td>'
                    . '<td>'.$QtyMutasi.'</td>'
                    . '<td>'.$QtyStock.'</td>'
                    . '<td>'.$QtyOpname.'</td>'
                    . '<td>'.$QtyAdjust.'</td>'
                    . '<td>'.$TotalAdjust.'</td>'
                    . '</tr>';
      //end foreach loop
    echo '</table>';

希望对您有所帮助。

【讨论】:

【参考方案5】:

具有键和值的递归循环是将数组打印为所需表格格式的最佳方式。

【讨论】:

【参考方案6】:

你可以使用

<?php $x=0; foreach ($datadetail as $data)  $x++;
$value= $data->QtyBeginning + $data->QtyIn + $data->QtyOut;

?>
                             <tr>
                                <td><?=$x;?></td>
                                <td><?=$data->ItemCode;?></td>
                                <td><?=$value;?></td>
                                <td><?=$data->ItemName;?></td>
                                <td><?=$data->StokOpnameTransNo;?></td>
                                <td><?=$data->OriginalPrice;?></td>
                                <td><?=$data->Price;?></td>
                                <td><?=$data->QtyBegining;?></td>
                                <td><?=$data->QtyIn;?></td>
                                <td><?=$data->QtySales;?></td>
                                <td><?=$data->QtyMutasi;?></td>
                                <td><?=$data->QtyStock;?></td>
                                <td><?=$data->QtyOpname;?></td>
                                <td><?=$data->QtyAdjust;?></td>
                                <td><?=currency($data->TotalAdjust);?></td>
                                <td><?=$data->rak;?></td>
                                <td><?=$data->line;?></td>
                            </tr>
                        <?php  ?>

【讨论】:

以上是关于按数组中的指定列对行进行分组的主要内容,如果未能解决你的问题,请参考以下文章

按 ID 和更新列对 SQL 中的行进行分组

Laravel 8:按嵌套列对分组数组进行排序 - 我应该在 sortBy 闭包中放入啥?

如何按列对pyspark中的数据框进行分组并以该列作为键并以记录列表作为其值来获取字典?

如何按r中其他列中的条件对行进行排序?

根据 r 中的其他列对一个列表中的一列的所有值进行分组

按 2 列对表进行分组并优先考虑非空值?