如何在 PHP 中动态显示数组中的此类数据
How to Dynamically display this kind of data from an Array in PHP
$branch = [
'7232' => [
'name' => 'United State Branch',
'branch_id' => 7232
],
'5431' => [
'name' => 'Brazil Branch',
'branch_id' => 5431
];
在分支的每个循环中,我想获取分支率数据并显示它以与示例图像对齐。 (只是显示是我所需要的,不能只是想办法在每个循环中很好地对齐 html。
SAMPLE DATA OF RATES after getting it from DB
$getRate = [
'0' => [
'kg' => '1',
'amount' => 50,
'branch_id' => 7232
],
'1' => [
'kg' => '2',
'amount' => 110,
'branch_id' => 7232
];
请社区,如何根据附图将其对齐好??
谢谢
有必要 assemble 一个新的嵌套数据结构,它可以让您以正确的顺序获取所需的数据。收集这样一个结构的例子:
// kg => branch_id => item
$indexedItems = [];
foreach ($getRate as $item) {
$kg = $item['kg'];
$branchId = $item['branch_id'];
$indexedItems[$kg][$branchId] = $item;
}
然后生成的结构可以用来构建,例如,table:
<table>
<thead>
<tr>
<?php foreach ($branch as $column): ?>
<th><?= $column['name'] ?></th>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($indexedItems as $row): ?>
<tr>
<?php foreach ($branch as $column): ?>
<?php $item = $row[$column['branch_id']]; ?>
<td><?= $item['kg'] ?>kg - $<?= $item['amount'] ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
谢谢大家,非常感谢大家的贡献和 @7-zete-7。
遵循相同的程序并能够解决它。这对我来说有点棘手。
Below is a full replica of the code;
// kg => branch_id => item
$indexedItems = [];
<thead>
<tr>
<?php foreach(GLOBALS::init('lh')['branch'] as $value) :?>
<th><?php echo $value['name'];?></th>
<?php
$getRate = getRatesByBranch($value['branch_id']);
$r_currency = getCurrencyData($value['currency'])['symbol'];
?>
<?php foreach($getRate as $key => $item) :?>
<?php
$item['currency'] = $r_currency;
$indexedItems[$key][$item['branch_id']] = $item;
?>
<?php endforeach;?>
<?php endforeach;?>
</tr>
</thead>
<tbody>
<?php foreach ($indexedItems as $row): ?>
<tr>
<?php foreach (GLOBALS::init('lh')['branch'] as $column): ?>
<?php
$item = NULL;
if(isset($row[$column['branch_id']]))
$item = $row[$column['branch_id']];
?>
<?php if(!is_null($item)) :?>
<td><?= $item['kg'] ?>kg - <?= "{$item['currency']} {$item['amount']}" ?></td>
<?php endif;?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
Thank you all once again and Happy to be here!!!
$branch = [
'7232' => [
'name' => 'United State Branch',
'branch_id' => 7232
],
'5431' => [
'name' => 'Brazil Branch',
'branch_id' => 5431
];
在分支的每个循环中,我想获取分支率数据并显示它以与示例图像对齐。 (只是显示是我所需要的,不能只是想办法在每个循环中很好地对齐 html。
SAMPLE DATA OF RATES after getting it from DB
$getRate = [
'0' => [
'kg' => '1',
'amount' => 50,
'branch_id' => 7232
],
'1' => [
'kg' => '2',
'amount' => 110,
'branch_id' => 7232
];
请社区,如何根据附图将其对齐好?? 谢谢
有必要 assemble 一个新的嵌套数据结构,它可以让您以正确的顺序获取所需的数据。收集这样一个结构的例子:
// kg => branch_id => item
$indexedItems = [];
foreach ($getRate as $item) {
$kg = $item['kg'];
$branchId = $item['branch_id'];
$indexedItems[$kg][$branchId] = $item;
}
然后生成的结构可以用来构建,例如,table:
<table>
<thead>
<tr>
<?php foreach ($branch as $column): ?>
<th><?= $column['name'] ?></th>
<?php endforeach ?>
</tr>
</thead>
<tbody>
<?php foreach ($indexedItems as $row): ?>
<tr>
<?php foreach ($branch as $column): ?>
<?php $item = $row[$column['branch_id']]; ?>
<td><?= $item['kg'] ?>kg - $<?= $item['amount'] ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
谢谢大家,非常感谢大家的贡献和 @7-zete-7。 遵循相同的程序并能够解决它。这对我来说有点棘手。
Below is a full replica of the code;
// kg => branch_id => item
$indexedItems = [];
<thead>
<tr>
<?php foreach(GLOBALS::init('lh')['branch'] as $value) :?>
<th><?php echo $value['name'];?></th>
<?php
$getRate = getRatesByBranch($value['branch_id']);
$r_currency = getCurrencyData($value['currency'])['symbol'];
?>
<?php foreach($getRate as $key => $item) :?>
<?php
$item['currency'] = $r_currency;
$indexedItems[$key][$item['branch_id']] = $item;
?>
<?php endforeach;?>
<?php endforeach;?>
</tr>
</thead>
<tbody>
<?php foreach ($indexedItems as $row): ?>
<tr>
<?php foreach (GLOBALS::init('lh')['branch'] as $column): ?>
<?php
$item = NULL;
if(isset($row[$column['branch_id']]))
$item = $row[$column['branch_id']];
?>
<?php if(!is_null($item)) :?>
<td><?= $item['kg'] ?>kg - <?= "{$item['currency']} {$item['amount']}" ?></td>
<?php endif;?>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
Thank you all once again and Happy to be here!!!