首页 > 解决方案 > PHP 数组:盒子和 I'm trying to code a script that allows you to generate a with the cities of each country. Cities should be grouped by country through PHP array. So this example here, in HTML, it is what I wanted but in PHP Array. <sele

问题描述

标签: phparraysselectoptgroup

解决方案


将城市分割成块:

$cities = array_chunk($cities, 5);
$counter = 0;?>

<select name="places">
<?php foreach($countries as $key): ?>
        <optgroup label="<?php echo $key; ?>">
        <?php foreach($cities[$counter++] as $key => $value): ?>
            <?php echo '<option value="'.$key.'">'.$value.'</option>'; ?>
            <?php endforeach; ?>
        </optgroup>
<?php endforeach; ?>
</select>

推荐阅读