2011-06-24 54 views
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; 
     } 
+0

我不确定我是否理解你正在尝试做什么......如果我明白了这一点,你想在**之前插入新的按钮**,**之后插入新的按钮**,或者作为**孩子父母的**?如果是这样,插入逻辑是错误的;你可能想看看[array_slice()](http://www.php.net/manual/en/function.array-slice.php) – alxbl

+0

是的,这正是我想要做的,什么是错的它的逻辑? – SoLoGHoST

+0

但是如何在关联数组上使用'array_slice'?可能吗? – SoLoGHoST

回答

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数组中的高效插入。

我希望这可以帮助你。

+0

这很好,但你能给我一个如何在之前和/或之后这样做的例子吗? – SoLoGHoST