关联数组排序不正确的问题

发布于 2024-11-16 23:49:13 字数 3643 浏览 2 评论 0原文

好的,我有一个像这样设置的数组:

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

Ok, I have an array set up like so:

$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(
        ),
    ),
);

Than I call a function after this is loaded to add some more buttons to it and have it sorted correctly, like this:

$buttons = load_dream_menu($buttons);

The load_dream_menu function looks like this:

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

Ok, so it manages to sort the first one but doesn't sort the other one's after that? Is there something I'm supposed to use within the foreach loop of the load_dream_menu function? Something like using reset(), but that doesn't seem to work either. What am I doing wrong here? Please someone help me.

So basically, I check the database, than I loop through all available menu items and add them into another array, than at the end, I set the original array ($buttons) to the newly created array. Shouldn't this work? Here is where I do this within load_dream_menu() function:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无可置疑 2024-11-23 23:49:13

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
}

,然后您可以在排序例程中使用这些函数。

我发现这篇有用的帖子< /a> 关于 PHP 数组中的高效插入。

我希望这对你有帮助。

PHP does not have functions out of the box to insert elements after or before an indexed key.

The current problem with your function is that when you read the first row, you will put back all the previous menu buttons into the new menu buttons. After that, there is no way to insert before or after, short of rebuilding the array. I would suggest writing helper functions like

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
}

Then you can use those functions in your sorting routine.

I found this helpful post about efficient inserts in PHP arrays.

I hope this helps you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文