我有一个如下的多维数组:我试图将数组拆分为多个较小的数组,然后可以将它们循环到基于不同的表中。如何将它们分开?
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
)
)
)
我尝试了下面的代码,它似乎按照我想要的方式拆分了数组,但是我不知道如何回显单个对象。
<?php foreach ($post_data as $account_group):
print "<pre>";
print_r($account_group);
print "</pre>";
endforeach; ?>
这是结果:
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
)
)
我希望这是有道理的。
答案 0 :(得分:0)
谢谢,我想过了。
这是我的代码,如果它可以帮助其他人。
<?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; ?>