循环到多维数组中,并建立具有行跨度的表 [英] Loop into a multidimensional array and built a table with rowspan

查看:88
本文介绍了循环到多维数组中,并建立具有行跨度的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个数组:

Array
(
    [France] => Array
        (
            [0] => Array
                (
                    [city] => Paris
                )
        )
    [Canada] => Array
        (
            [0] => Array
                (
                    [city] => Montreal
                )

            [1] => Array
                (
                    [city] => Ottawa
                )
        )
)

有时候,就像您看到一个国家可以拥有一个城市(以法国为例),但有时该国家可以拥有多个城市(以加拿大为例)。

Sometimes, like you can see a country can have one city (case for France) but sometimes the country can have more than one city (case for Canada).

我希望获得最终输出:

<table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Cities</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td rowspan="1">France</td>
            <td>Paris</td>
        </tr>
        <tr>
            <td rowspan="2">Canada</td>
            <td>Montreal</td>
        </tr>
        <tr>
            <td>Toronto</td>
        </tr>
    </tbody>
</table>

这实际上是我的意思:

foreach($countries as $country => $city) {
    $count = count($country) ;

    if($count == 1) {
        echo '
            <tr>
                <td rowspan="1">'.$country.'</td>
                <td>'.$city.'</td>
            </tr>
        '
    }
    else {
        echo '
            <tr>
                <td rowspan="'.$count.'">'.$country.'</td>
                <td>'.$city.'</td>
            </tr>
            <tr>
                <td>'.$city.'</td>
            </tr>
        '
    }      
}

我的问题是循环和

谢谢您的帮助。

推荐答案

由于要为每个城市创建一行,因此必须在每个城市上循环。确定行跨度值以及是否在同一行上显示第一个城市只是基于其所在国家的城市是否是第一次迭代。

Because you are creating a row for each city, you must loop over each city. Determining the rowspan value and whether to show the first city on the same row is simply based upon whether its the first iteration of the country's cities.

因此,将产生以下代码您期望的结果:

So the following code will produce your desired result:

<?php
$array = [
    'France' => [
        [
            'city' => 'Paris'    
        ]    
    ], 
    'Canada' => [
        [
            'city' => 'Montreal'    
        ], [
            'city' => 'Ottawa'    
        ],
    ],
];
?>
<table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Cities</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach ($array as $country => $city): ?>
        <?php foreach (array_values($city) as $i => $value): ?>
        <tr>
            <?php if ($i === 0): ?>
            <td rowspan="<?= count($city) ?>"><?= $country ?></td>
            <?php endif ?>
            <td><?= $value['city'] ?></td>
        </tr>
        <?php endforeach ?>
        <?php endforeach ?>
    </tbody>
</table>

https://3v4l.org/vp2bl

结果:

<table>
    <thead>
        <tr>
            <th>Country</th>
            <th>Cities</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td rowspan="1">France</td>
            <td>Paris</td>
        </tr>
        <tr>
            <td rowspan="2">Canada</td>
            <td>Montreal</td>
        </tr>
        <tr>
            <td>Ottawa</td>
        </tr>
    </tbody>
</table>

这篇关于循环到多维数组中,并建立具有行跨度的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆