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条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改