按数组

时间:2017-03-24 08:08:24

标签: php arrays codeigniter codeigniter-3

我有这个数组

   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中)更改为类似

的结果

enter image description here

就像ItemCode分组一样。在每个商品代码之后我想要总结QtyBeginning,QtyIn,QtyOut etc

6 个答案:

答案 0 :(得分:1)

我不知道您的表结构,但在我看来,您应该能够从数据库中组织数据

无论哪种方式,我都试图向您展示如何做到这一点(全文稿)

TESTDATA:

$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>

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

btw:您可以将ItemCollectionWrapper和ItemCollection类放在模型代码下面 如果您不知道该怎么做 - 告诉我您的模型和控制器,我会告诉您。

答案 1 :(得分:0)

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

第二:我有一个解决这个问题的方法,试试这个我希望有所帮助。 想法 - &gt;我尝试通过SQL比较来自数组返回的Item代码(你按照asc顺序对 Itemcode 进行mutch排序。

然后尝试代码 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>

答案 2 :(得分:0)

@ 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++;
           }
        }

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

答案 3 :(得分:0)

示例数据:感谢@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,或者我可以说按项目代码过滤数据

    $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>';

希望这可以帮助你。

答案 4 :(得分:0)

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

答案 5 :(得分:-1)

您可以使用

<?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 } ?>