【发布时间】:2022-01-24 18:24:35
【问题描述】:
我正在尝试更新一段旧代码(菜单构建器类)。我已经更新了其他所有内容,但我被困在使用 each() 函数的行上。我确实阅读了一些以前的线程,但是这个特定的实例太复杂了,我无法弄清楚如何更改。这里是:
while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) )
这是出现上述行的整个函数:
function get_menu_html($lang, $root_id = 0 )
{
$this->html = array();
$this->items = $this->get_menu_items($lang);
foreach ( $this->items as $item )
$children[$item['sectionParentID']][] = $item;
// loop will be false if the root has no children (i.e., an empty menu!)
$loop = !empty( $children[$root_id] );
// initializing $parent as the root
$parent = $root_id;
$parent_stack = array();
// HTML wrapper for the menu (open)
$this->html[] = '<ul>';
while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) )
{
if ( $option === false )
{
$parent = array_pop( $parent_stack );
// HTML for menu item containing children (close)
$this->html[] = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 ) . '</ul>';
$this->html[] = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 ) . '</li>';
}
elseif ( !empty( $children[$option['value']['sectionID']] ) )
{
$tab = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 );
// HTML for menu item containing children (open)
$this->html[] = sprintf(
'%1$s<li><a href="%2$s">%3$s</a>',
$tab, // %1$s = tabulation
$option['value']['sectionPage'], // %2$s = sectionPage (URL)
$option['value']['sectionLabel'] // %3$s = title
);
$this->html[] = $tab . "\t" . '<ul class="submenu">';
array_push( $parent_stack, $option['value']['sectionParentID'] );
$parent = $option['value']['sectionID'];
}
else
// HTML for menu item with no children (aka "leaf")
$this->html[] = sprintf(
'%1$s<li><a href="%2$s">%3$s</a></li>',
str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 ), // %1$s = tabulation
$option['value']['sectionPage'], // %2$s = sectionPage (URL)
$option['value']['sectionLabel'] // %3$s = title
);
}
编辑:添加屏幕截图以显示此代码和以下两个建议产生的内容。
编辑 2:如果您想用我自己的数据进行测试,这是我使用 get_menu_items($lang) 从我的数据库中得到的打印结果:
Array ( [0] => Array ( [sectionID] => 1 [sectionParentID] => 0 [sectionPage] => Home [sectionLabel] => Начало ) [1] => Array ( [sectionID] => 2 [sectionParentID] => 0 [sectionPage] => Translations [sectionLabel] => Преvоди ) [2] => Array ( [sectionID] => 3 [sectionParentID] => 0 [sectionPage] => Prose [sectionLabel] => Проzа ) [3] => Array ( [sectionID] => 4 [sectionParentID] => 0 [sectionPage] => Poetry [sectionLabel] => Поезiя ) [4] => Array ( [sectionID] => 5 [sectionParentID] => 3 [sectionPage] => Stories [sectionLabel] => Разкази ) [5] => Array ( [sectionID] => 6 [sectionParentID] => 3 [sectionPage] => Articles [sectionLabel] => Статии ) [6] => Array ( [sectionID] => 7 [sectionParentID] => 3 [sectionPage] => Essays [sectionLabel] => Есета ) [7] => Array ( [sectionID] => 8 [sectionParentID] => 3 [sectionPage] => Fragments [sectionLabel] => Фрагменти ) [8] => Array ( [sectionID] => 9 [sectionParentID] => 4 [sectionPage] => Woman [sectionLabel] => Аз и Жената ) [9] => Array ( [sectionID] => 10 [sectionParentID] => 4 [sectionPage] => Civilization [sectionLabel] => Аз и Цивилизацията ) [10] => Array ( [sectionID] => 11 [sectionParentID] => 4 [sectionPage] => Universe [sectionLabel] => Аз и Вселената ) [11] => Array ( [sectionID] => 12 [sectionParentID] => 4 [sectionPage] => Duskoreznitsa [sectionLabel] => (По-)Етична дъскорезница ) [12] => Array ( [sectionID] => 13 [sectionParentID] => 4 [sectionPage] => PrazniPrikazki [sectionLabel] => Празни приказки ) [13] => Array ( [sectionID] => 14 [sectionParentID] => 4 [sectionPage] => Extrakts [sectionLabel] => Екстракти ) [14] => Array ( [sectionID] => 17 [sectionParentID] => 0 [sectionPage] => Blog [sectionLabel] => Трънки и блогинkи ) [15] => Array ( [sectionID] => 18 [sectionParentID] => 0 [sectionPage] => Contact [sectionLabel] => Контакт ) [16] => Array ( [sectionID] => 19 [sectionParentID] => 0 [sectionPage] => About [sectionLabel] => За сайта ) [17] => Array ( [sectionID] => 20 [sectionParentID] => 3 [sectionPage] => Research [sectionLabel] => Изследвания ) [18] => Array ( [sectionID] => 21 [sectionParentID] => 3 [sectionPage] => Amsterdamned [sectionLabel] => Amsterdamned ) [19] => Array ( [sectionID] => 22 [sectionParentID] => 4 [sectionPage] => Treski [sectionLabel] => Трески ) [20] => Array ( [sectionID] => 23 [sectionParentID] => 2 [sectionPage] => PoetryTranslation [sectionLabel] => Преводи на поезия ) [21] => Array ( [sectionID] => 25 [sectionParentID] => 2 [sectionPage] => ProseTranslation [sectionLabel] => Преводи на проза ) [22] => Array ( [sectionID] => 27 [sectionParentID] => 2 [sectionPage] => SubtitleTranslation [sectionLabel] => Преводи на субтитри ) [23] => Array ( [sectionID] => 28 [sectionParentID] => 2 [sectionPage] => OpinionJournalismTranslation [sectionLabel] => Преводи на публицистика ) )
parent_id 替换为 sectionParentID
id 替换为 sectionID
link 替换为 sectionPage
title 替换为 sectionLabel。
【问题讨论】:
-
如果您运行代码,它是否仍表示已弃用?
-
你能发布一个函数的极简隔离示例,使用“每个”的示例输入和输出吗?循环是相当复杂的,它如何使用每个迭代并与它的父级一起玩,并且可能是一堆意大利面条。理想情况下,这可以重构为更易读的东西,比如首先循环父母,这样可以构建一个更流畅的循环,这样人类就不会那么复杂了。在一个孤立的例子中,你可以给我们整个函数。
-
@Dean:是的,我看到这条消息:“已弃用:each() 函数已弃用。此消息将在 C:... 的进一步调用中被禁止显示”
-
@KevinY:我很难真正理解这个功能。如果我确切地知道它的作用,我也许可以写一个新的来达到同样的效果。
-
@KevinY,请在这篇文章中找到原始代码(我从其他地方得到的,现在找不到了):stackoverflow.com/questions/29892308/…
标签: php each deprecated