拥有php数组
<div>
<ul class="mnav-ul">
<li class="mnav-ul-li">Contacts</li>
<li class="mnav-ul-li">Services
<ul class="mnav-ul-ul">
<li>Legal services</li>
<li>Accounting services</li>
</ul>
</li>
</ul>
</div>
<div style="clear:both"></div>
从数组想要得到像这样的HTML
UpperLevelNumberRenamed
我做了
1)创建了一个顶级菜单项数组,另一个包含子级菜单项的数组和包含foreach ( $navigation as $k_navigation => $v_navigation ){
if( $v_navigation['UpperLevelNumberRenamed'] == 0 ){
$top_level_navigation[] = $v_navigation;
}
else if( $v_navigation['UpperLevelNumberRenamed'] != 0 ){
$sub_level_navigation[] = $v_navigation;
$sublev_upper_level_number_renamed[] = $v_navigation['UpperLevelNumberRenamed'];
}//else if( $v_navigation['UpperLevelNumberRenamed'] != 0 ){
}//foreach ( $navigation as $k_navigation => $v_navigation ){
个子级菜单项的数组
$main_navigation = '
<div>
<ul class="mnav-ul">
';
foreach ( $top_level_navigation as $k_top_level_navigation => $v_top_level_navigation ){
if( !in_array( $v_top_level_navigation['NumberRenamed'], $sublev_upper_level_number_renamed ) ) {
$main_navigation .= '<li class="mnav-ul-li">'. $v_top_level_navigation['TopicName']. '</li>';
}
else if( in_array( $v_top_level_navigation['NumberRenamed'], $sublev_upper_level_number_renamed ) ) {
$main_navigation .= '<li class="mnav-ul-li">'. $v_top_level_navigation['TopicName']. '';
$main_navigation .= '<ul class="mnav-ul-ul">';
foreach ( $sub_level_navigation as $k_sub_level_navigation => $v_sub_level_navigation ){
if( $v_top_level_navigation['NumberRenamed'] == $v_sub_level_navigation['UpperLevelNumberRenamed'] ){
$main_navigation .= '<li>'. $v_sub_level_navigation['TopicName']. '</li>';
}
}//foreach ( $sub_level_navigation as $k_sub_level_navigation => $v_sub_level_navigation ){
$main_navigation .= '</ul></li>';
}//if( ( in_array( $v_top_level_navigation['NumberRenamed'], $sublev_upper_level_number_renamed ) ) {
}//foreach ( $navigation as $k_navigation => $v_navigation ){
$main_navigation .= '</ul>
</div>
<div style="clear:both"></div>';
echo $main_navigation;
2)然后首先循环通过顶级项目数组,然后在每个循环内循环再次循环通过子级别菜单项目
INSERT INTO users (name, age) VALUES ('Liszt', 10) RETURNING id;
可以得到我需要的东西。但是循环太多(可能浪费系统资源)。是否可以通过更少的循环获得相同的结果(使用更少的系统重复)?