简体   繁体   English

如何在 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
 ];

On each loop of the branch i want to get the branch rates data and display this to align with the sample image.在分支的每个循环上,我想获取分支速率数据并将其显示为与示例图像对齐。 (Just the display is what i need, and can't just figure a way to align the html well on each loop. (只是显示是我需要的,不能只是想办法在每个循环上很好地对齐 html。

SAMPLE DATA OF RATES after getting it from DB从 DB 获取后的费率样本数据

$getRate = [
     '0' => [
            'kg' => '1',
            'amount' => 50,
            'branch_id' => 7232
     ],
     '1' => [
            'kg' => '2',
            'amount' => 110,
            'branch_id' => 7232
     ];

Please Community, how can one align this well according to the attached image??请社区,如何根据附图很好地对齐这个? Thanks谢谢

It is necessary to assemble a new nesting data structure which will allow you to get the required data in the correct order.有必要组装一个新的嵌套数据结构,它可以让您以正确的顺序获取所需的数据。 An example of collecting such a structure:收集此类结构的示例:

// kg => branch_id => item
$indexedItems = [];

foreach ($getRate as $item) {
    $kg = $item['kg'];
    $branchId = $item['branch_id'];

    $indexedItems[$kg][$branchId] = $item;
}

Then the resulting structure can be used to build, for example, a 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>

Thanks all, am very grateful to all contribution and according to @7-zete-7 .谢谢大家,非常感谢所有贡献和@7-zete-7 Followed same procedure and was able to resolve it.遵循相同的程序并能够解决它。 It was a bit tricky for me.这对我来说有点棘手。

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!!!再次感谢大家,很高兴来到这里!!!

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

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