简体   繁体   English

如何在视图中回显多维数组-Codeigniter

[英]How to echo multidimensional array in view - Codeigniter

I have a multidimensional array like below: I am trying to split the array into multiple smaller arrays that I can then can have loop into different tables based. 我有一个如下的多维数组:我试图将数组拆分为多个较小的数组,然后可以将其循环到基于不同的表中。 How do I split these up? 我该如何拆分?

 Array
(
 [COGS-Service] => Array
    (
        [0] => stdClass Object
            (
                [id] => 109
                [date] => 2019-01-29 15:26:17
                [description] => test2
                [account] => 2
                [user] => 4
                [quantity] => 1
                [cost] => 1.00
                [transfer_from] => 1
                [transfer_to] => 2
                [status] => 3
                [account_name] => COGS-Service
            )

        [1] => stdClass Object
            (
                [id] => 113
                [date] => 2019-01-30 13:55:25
                [description] => test3
                [account] => 2
                [user] => 4
                [quantity] => 1
                [cost] => 1.00
                [transfer_from] => 1
                [transfer_to] => 2
                [status] => 3
                [account_name] => COGS-Service
            )

    )

[COGS-Store] => Array
    (
        [0] => stdClass Object
            (
                [id] => 111
                [date] => 2019-01-29 15:25:55
                [description] => test richfield
                [account] => 1
                [user] => 4
                [quantity] => 1
                [cost] => 1.00
                [transfer_from] => 2
                [transfer_to] => 1
                [status] => 3
                [account_name] => COGS-Store
            )

    )

)

I have tried the below code and it seems to split the arrays the way I would like but I don't know how to echo the individual objects. 我尝试了下面的代码,它似乎以我想要的方式拆分了数组,但是我不知道如何回显单个对象。

<?php foreach ($post_data as $account_group):
    print "<pre>";
    print_r($account_group);
    print "</pre>";
endforeach; ?>

Here is the result: 结果如下:

 Array
 (
 [0] => stdClass Object
    (
        [id] => 109
        [date] => 2019-01-29 15:26:17
        [description] => test2
        [account] => 2
        [user] => 4
        [quantity] => 1
        [cost] => 1.00
        [transfer_from] => 1
        [transfer_to] => 2
        [status] => 3
        [account_name] => COGS-Service
    )

[1] => stdClass Object
    (
        [id] => 113
        [date] => 2019-01-30 13:55:25
        [description] => test3
        [account] => 2
        [user] => 4
        [quantity] => 1
        [cost] => 1.00
        [transfer_from] => 1
        [transfer_to] => 2
        [status] => 3
        [account_name] => COGS-Service
    )

)
Array
 (
[0] => stdClass Object
    (
        [id] => 111
        [date] => 2019-01-29 15:25:55
        [description] => test richfield
        [account] => 1
        [user] => 4
        [quantity] => 1
        [cost] => 1.00
        [transfer_from] => 2
        [transfer_to] => 1
        [status] => 3
        [account_name] => COGS-Store
    )

)

I hope this makes sense. 我希望这是有道理的。 Pretty new at this. 在这很新。

Thanks, I way over thought this. 谢谢,我想这个。

Here is my code if it helps anyone else. 这是我的代码,如果它可以帮助其他人。

<?php if (empty($post_data)): ?>
        <?php echo '<h2><strong>No Current Transfers!!</strong></h2>'; ?> 
            <?php else: ?>
        <?php foreach ($post_data as $account_group => $key): ?>
            <div class="panel panel-default">
            <div class="panel-heading"><strong><?php echo $account_group; ?></strong></div>
            <div class="panel-body">
                    <table id="<?php echo $account_group=preg_replace('/\s+/', '', $account_group); ?>" class="table table-hover text-centered">
    <thead>
        <tr>
            <th class="hidden-xs">Date</th>
            <th>Description</th>
            <th>Qty</th>
            <th>Cost</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>

                 <?php if (is_array($key)):
                        foreach ($key as $account_data): ?>
                <tr>
            <td class="hidden-xs" style="white-space: nowrap"><?php echo date("m-d-y",strtotime($account_data->date)); ?></td>
            <td><?php echo $account_data->description; ?></td>
            <td><?php echo $account_data->quantity; ?></td>
            <td><?php echo "$". $account_data->cost; ?></td>
            <td><?php echo "$". sprintf("%0.2f",$account_data->quantity * $account_data->cost); ?></td>    
                </tr>   


<?php endforeach; ?>
    <?php endif; ?>
<tfoot>
        <tr>

        <th class="hidden-xs"></th>
        <th></th>
        <th></th>
        <th>Total:</th>
        <th></th>


        </tr>
 </tfoot> 
    </tbody>
    </table>

         </div>
            </div>

         <?php endforeach; ?>

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

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