如何在单个 table 中并行显示两个不同的数组,如 Php 中的 Left 和 Right

How to display two different arrays in a single table in parallel like Left and Right in Php

我有 2 个数组 $expense 和 $income。

收入数组

Array
(
    [0] => Array
        (
            [head] => chitty_installment
            [amount] => 500.00
        )

    [1] => Array
        (
            [head] => sales
            [amount] => 100000.00
        )

)

支出数组

Array
(
    [0] => Array
        (
            [head] => purchase
            [amount] => 2000.00
        )

    [1] => Array
        (
            [head] => sales-return
            [amount] => 9000.00
        )
    [2] => Array
        (
            [head] => salary 
            [amount] => 11000.00
        )

)

我正在尝试打印这些详细信息,例如 table 左侧的费用和右侧的收入 那就是 table 看起来像,

Expense Amount Income Amount
purchase 2000.00 chitty_installment 500.00
sales-return 9000.00 sales 100000.00
salary 11000.00

    <table class="table table-striped">
        <thead>
            <tr>
                <th><b>EXPENDITURE</b></th>
                <th><b>AMOUNT</b></th>
                <th><b>INCOME</b></th>
                <th><b>AMOUNT</b></th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($expense as $exp) { ?>
            <tr>
                <td><?php echo $exp['head']; ?></td>
                <td><?php echo $exp['amount']; ?></td>
            <?php foreach ($income as $inc) { ?>
    
                <td><?php echo $inc['head']; ?></td>
                <td><?php echo $inc['amount'] ?></td>
            </tr>
            <?php }
                } ?>
        </tbody>
    </table>

不需要第二个 foreach(),使用第一个 foreach() 键从 income array

中获取值

也使用isset()检查income array中的值是否存在相应的键?

<table class="table table-striped">
    <thead>
        <tr>
            <th><b>EXPENDITURE</b></th>
            <th><b>AMOUNT</b></th>
            <th><b>INCOME</b></th>
            <th><b>AMOUNT</b></th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($expense as $key=>$exp) { ?>
            <tr>
                <td><?php echo $exp['head']; ?></td>
                <td><?php echo $exp['amount']; ?></td>
    
                <td><?php echo isset($income[$key]['head']) ? $income[$key]['head'] : ''; ?></td>
                <td><?php echo isset($income[$key]['amount']) ? $income[$key]['amount'] : ''; ?></td>
            </tr>
        <?php } ?>
    </tbody>
</table>

作为替代方案,您可以在单个 foreach.

中使用 MultipleIterator 两个循环两个数组

只需添加标志 MultipleIterator::MIT_NEED_ANY,这样您就不必担心数组一的计数多于数组二或相反。

$it = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$it->attachIterator(new ArrayIterator($expense));
$it->attachIterator(new ArrayIterator($income));

然后像往常一样将它加载到 foreach 中。索引零表示 expense1 作为 income:

<?php foreach ($it as $k => $v) { ?>
    <tr>
        <td><?php echo $v[0]['head'] ?? ''; ?></td>
        <td><?php echo $v[0]['amount'] ?? ''; ?></td>
        <td><?php echo $v[1]['head'] ?? ''; ?></td>
        <td><?php echo $v[1]['amount'] ?? ''; ?></td>
    </tr>
<?php } ?>

Sample output