好的,我已阵列设置像这样:问题的一个关联数组不排序正确
$buttons = array(
'home' => array(
'title' => $txt['home'],
'href' => $scripturl,
'show' => true,
'sub_buttons' => array(
),
'is_last' => $context['right_to_left'],
),
'help' => array(
'title' => $txt['help'],
'href' => $scripturl . '?action=help',
'show' => true,
'sub_buttons' => array(
),
),
);
比我请在此之后被加载到一些更多的按钮添加到它的功能,并且具有它正确排序,像这样:
$buttons = load_dream_menu($buttons);
的load_dream_menu功能如下:
function load_dream_menu($menu_buttons)
{
global $smcFunc, $user_info, $scripturl, $context;
$request = $smcFunc['db_query']('', '
SELECT *
FROM {db_prefix}dp_dream_menu
ORDER BY id_button ASC',
array(
)
);
$new_menu_buttons = array();
while ($row = $smcFunc['db_fetch_assoc']($request))
{
$permissions = explode(',', $row['permissions']);
$dp_temp_menu = array(
'title' => $row['name'],
'href' => ($row['target'] == 'forum' ? $scripturl : '') . $row['link'],
'show' => (array_intersect($user_info['groups'], $permissions)) && ($row['status'] == 'active' || (allowedTo('admin_forum') && $row['status'] == 'inactive')),
'target' => $row['target'],
'active_button' => false,
);
foreach ($menu_buttons as $area => $info)
{
if ($area == $row['parent'] && $row['position'] == 'before')
$new_menu_buttons[$row['slug']] = $dp_temp_menu;
$new_menu_buttons[$area] = $info;
if ($area == $row['parent'] && $row['position'] == 'after')
$new_menu_buttons[$row['slug']] = $dp_temp_menu;
if ($area == $row['parent'] && $row['position'] == 'child_of')
$new_menu_buttons[$row['parent']]['sub_buttons'][$row['slug']] = $dp_temp_menu;
if ($row['position'] == 'child_of' && isset($info['sub_buttons']) && array_key_exists($row['parent'], $info['sub_buttons']))
$new_menu_buttons[$area]['sub_buttons'][$row['parent']]['sub_buttons'][$row['slug']] = $dp_temp_menu;
}
}
if (!empty($new_menu_buttons))
$menu_buttons = $new_menu_buttons;
return $menu_buttons;
}
好了,所以它设法排序的第一个,但没有排序其他人的后?有什么我应该在load_dream_menu函数的foreach循环中使用吗?类似于使用reset()
,但似乎也不起作用。我在这里做错了什么?请有人帮助我。因此基本上,我检查数据库,比遍历所有可用的菜单项并将它们添加到另一个数组中,而不是最后,我将原始数组($ buttons)设置为新创建的数组。不应该这样工作吗?下面是我做的这个地方load_dream_menu()函数中:
foreach ($menu_buttons as $area => $info)
{
if ($area == $row['parent'] && $row['position'] == 'before')
$new_menu_buttons[$row['slug']] = $dp_temp_menu;
$new_menu_buttons[$area] = $info;
if ($area == $row['parent'] && $row['position'] == 'after')
$new_menu_buttons[$row['slug']] = $dp_temp_menu;
if ($area == $row['parent'] && $row['position'] == 'child_of')
$new_menu_buttons[$row['parent']]['sub_buttons'][$row['slug']] = $dp_temp_menu;
if ($row['position'] == 'child_of' && isset($info['sub_buttons']) && array_key_exists($row['parent'], $info['sub_buttons']))
$new_menu_buttons[$area]['sub_buttons'][$row['parent']]['sub_buttons'][$row['slug']] = $dp_temp_menu;
}
我不确定我是否理解你正在尝试做什么......如果我明白了这一点,你想在**之前插入新的按钮**,**之后插入新的按钮**,或者作为**孩子父母的**?如果是这样,插入逻辑是错误的;你可能想看看[array_slice()](http://www.php.net/manual/en/function.array-slice.php) – alxbl
是的,这正是我想要做的,什么是错的它的逻辑? – SoLoGHoST
但是如何在关联数组上使用'array_slice'?可能吗? – SoLoGHoST