关联数组无法正确排序的问题

时间:2011-06-24 08:08:31

标签: php associative-array sorting

好的,我有一个这样的阵列设置:

$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;
        }

1 个答案:

答案 0 :(得分:1)

PHP没有开箱即用的功能,无法在索引键之后或之前插入元素。

您的功能当前的问题是,当您阅读第一行时,您将把所有以前的菜单按钮放回新的菜单按钮。之后,没有办法在重建阵列之前或之后插入。我建议编写像

这样的辅助函数
insert_before(array, key, value)
{
    // Splice array in two at key, keeping key on the right side
    // Append new value on the left tail
    // Glue both arrays into a new array
    // Return new array
}
insert_after(array, key, value)
{
    // Symmetric with right & left switched
}

然后您可以在排序例程中使用这些功能。

我发现this helpful post关于PHP数组中的高效插入。

我希望这会对你有所帮助。