doubeng1278 2014-11-26 07:41
浏览 24
已采纳

递归函数中的States / Cities数组,用于输出带有optgroup的select

I have an array of States/Cities where each element has a $cities['parent'] key (the value is 0 for States, and > 0 or Cities, like this:

$cities = array(
    '1' => array(
        'id' => '1',
        'parent' => '0',
        'name' => 'California',
    ),
    '2' => array(
        'id' => '2',
        'parent' => '0',
        'name' => 'Texas',
    ),
    '3' => array(
        'id' => '3',
        'parent' => '0',
        'name' => 'Florida',
    ),
    '4' => array(
        'id' => '4',
        'parent' => '1',
        'name' => 'Los Angeles',
    ),
    '5' => array(
        'id' => '5',
        'parent' => '1',
        'name' => 'San Francisco',
    ),
    '6' => array(
        'id' => '6',
        'parent' => '1',
        'name' => 'Sacramento',
    ),
    '7' => array(
        'id' => '7',
        'parent' => '2',
        'name' => 'Houston',
    ),
    '8' => array(
        'id' => '8',
        'parent' => '2',
        'name' => 'Dallas',
    ),
    '9' => array(
        'id' => '9',
        'parent' => '3',
        'name' => 'Miami',
    ),
    '10' => array(
        'id' => '10',
        'parent' => '3',
        'name' => 'Orlando',
    ),
)

I also have a recursive function to generate <select> options, like this:

function citiesSelect($ancestor, $array, $level, $selected = 0) {
    $has_children = false;
    foreach($array as $key => $value) {
        if ($value['parent'] == $ancestor) {       
            if ($has_children === false && $ancestor) {
                $has_children = true;
            }
            ?>
            <option value="<?php echo $value['id'] ?>" <?php echo ($value['id']) == $selected ? ' selected' : ''; ?>><?php echo $value['name']; ?></option>
            <?php
            citiesSelect($key, $array, $level, $selected);
        }
    }
}

And I recall with this:

<select>
    <?php citiesSelect(0, $cities, 0); ?>
</select>

This is fine (here is a working fiddle: http://phpfiddle.org/main/code/uiax-n03q) but here comes a problem: I'd like to put States in an <optroup> so they are not selectable, and resulting in a <select> like this:

<select>
    <optgroup label="California">
        <option value="4">Los Angeles</option>
        <option value="5">San Francisco</option>
        <option value="6">Sacramento</option>
    </optgroup>
    <optgroup label="Texas">
        <option value="7">Houston</option>
        <option value="8">Dallas</option>
    </optgroup>
    <optgroup label="Florida">
        <option value="9">Miami</option>
        <option value="10">Orlando</option>
    </optgroup>
</select>

That's fine: how to achieve this?

Thanks in advance

EDIT: thanks to answers that suggest to split into 2 arrays, but sorry I can't modify it! The array yet serves successfully another classic menu (<ul>...</ul>) and anyway I could have a third level like:

'11' => array(
    'id' => '11',
    'parent' => '4',
    'name' => 'Hollywood',
),
'12' => array(
    'id' => '12',
    'parent' => '4',
    'name' => 'Santa Monica',
)
  • 写回答

5条回答 默认 最新

  • dongshanxiao7328 2014-11-26 09:48
    关注

    At last I sorted it out with this function (also improved a little bit, to deal with indentation for nested third/fourth/etc. levels):

    function citiesSelect($parent, $array, $level, $selected = 0) {
        $has_children = false;
        foreach($array as $key => $value) {
            if ($value['parent'] == $parent) {       
                if ($has_children === false && $parent) {
                    $has_children = true;
                    $level += 1;
                } else if ($parent == 0) {
                    echo '<optgroup label="' . $value['name'] . '">' . "
    ";    
                }
                if ($parent > 0) { ?>
                <option value="<?php echo $value['id'] ?>" <?php echo ($value['id']) == $selected ? 'selected' : ''; ?>>
                    <?php echo str_repeat('&rsaquo; ',$level);?><?php echo $value['name']; ?>
                </option>
                <?php
                }
                citiesSelect($key, $array, $level);
                if ($parent == 0) { echo '</optgroup>'; };
            }
        }
    }
    

    And here is the working fiddle

    Thanks to you all for the patience and the time spent

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)