【发布时间】:2014-01-22 18:56:11
【问题描述】:
我有一个数组如下,我想构建一个嵌套的 Select DropDown 选项
array(
(int) 0 => array(
'ProductCategory' => array(
'id' => '5',
'name' => 'Mud',
'parent_id' => '0'
),
'children' => array(
(int) 0 => array(
'ProductCategory' => array(
'id' => '6',
'name' => 'Sand',
'parent_id' => '5'
),
'children' => array(
(int) 0 => array(
'ProductCategory' => array(
'id' => '7',
'name' => 'Soil',
'parent_id' => '6'
),
'children' => array()
)
)
)
)
),
(int) 1 => array(
'ProductCategory' => array(
'id' => '8',
'name' => 'House',
'parent_id' => '0'
),
'children' => array(
(int) 0 => array(
'ProductCategory' => array(
'id' => '9',
'name' => 'Door',
'parent_id' => '8'
),
'children' => array(
(int) 0 => array(
'ProductCategory' => array(
'id' => '11',
'name' => 'Latch',
'parent_id' => '9'
),
'children' => array()
),
(int) 1 => array(
'ProductCategory' => array(
'id' => '12',
'name' => 'Glass',
'parent_id' => '9'
),
'children' => array(
(int) 0 => array(
'ProductCategory' => array(
'id' => '13',
'name' => 'Semi ',
'parent_id' => '12'
),
'children' => array()
)
)
)
)
),
(int) 1 => array(
'ProductCategory' => array(
'id' => '10',
'name' => 'Window',
'parent_id' => '8'
),
'children' => array()
)
)
)
)
我正在寻找一种有效的方法来循环和创建 HTML 代码,如下所示。我不确定它会有多少级。所以我需要一个循环证明。
<select>
<optgroup label="Level One">
<option> A.1 </option>
<optgroup label="Level Two">
<option> A.B.1 </option>
</optgroup>
<option> A.2 </option>
</optgroup>
</select>
通过下拉菜单的嵌套外观示例
News
--Sport
----Sky
----Football
------Football-1
--War
----War1
--tech
Article
--tech
----tech2
------tech3
--php
--linux
【问题讨论】:
标签: php html cakephp option optgroup