简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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