php sorting array gives: Undefined offset: 6

时间:2016-04-04 18:18:43

标签: php arrays sorting undefined

I am trying to sort out the array on orders, but when i use sort or sort array etc its gives me a undefined offset error. Without sorting it works perfect!

Script:

    class Dynamic_menu {

private $ci;                // for CodeIgniter Super Global Reference.
private $id_menu        = 'class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"';
private $class_menu        = 'class="nav navbar-nav"';
private $class_parent    = 'class="parent"';
private $class_last        = 'class="last"';

// --------------------------------------------------------------------

/**
 * PHP5        Constructor
 *
 */
function __construct()
{
    $this->ci =& get_instance();    // get a reference to CodeIgniter.
}

// --------------------------------------------------------------------

/**
 * build_menu($table, $type)
 *
 * Description:
 *
 * builds the Dynaminc dropdown menu
 * $table allows for passing in a MySQL table name for different menu tables.
 * $type is for the type of menu to display ie; topmenu, mainmenu, sidebar menu
 * or a footer menu.
 *
 * @param    string    the MySQL database table name.
 * @param    string    the type of menu to display.
 * @return    string    $html_out using CodeIgniter achor tags.
 */
function build_menu($menu_id)
{
    $menu = array();
    // use active record database to get the menu.

    //$this->ci->db->order_by('orders', 'asc');
    //$query = $this->ci->db->get('menus_data');
    //$query = $this->ci->db->get($table);
    $this->ci->db->select("*");
    $this->ci->db->from("menus_data");
   // $this->ci->db->where("menu_id", $menu_id);
    $query = $this->ci->db->get();

    if ($query->num_rows() > 0)
    {
        // `id`, `title`, `link_type`, `page_id`, `module_name`, `url`, `uri`, `dyn_group_id`, `position`, `target`, `parent_id`, `show_menu`

        foreach ($query->result() as $row)
        {
            $menu[$row->id]['id']            = $row->id;
            $menu[$row->id]['title']        = $row->menu_title;
            //$menu[$row->id]['link']            = $row->link_type;
            $menu[$row->id]['page']            = $row->page_id;
            //$menu[$row->id]['module']        = $row->module_name;
            $menu[$row->id]['url']            = $row->menu_url;
            //$menu[$row->id]['uri']            = $row->uri;
            //$menu[$row->id]['dyn_group']    = $row->dyn_group_id;
            $menu[$row->id]['orders']        = $row->orders;
            //$menu[$row->id]['target']        = $row->target;
            $menu[$row->id]['parent']        = $row->parent_id;
            $menu[$row->id]['is_parent']    = $row->is_parent;
            $menu[$row->id]['show']            = $row->visable;
        }
    }
    $query->free_result();    // The $query result object will no longer be available



    // ----------------------------------------------------------------------
    // now we will build the dynamic menus.
    $html_out  = "\t".'<div '.$this->id_menu.'>'."\n";
    $html_out .= "\t\t".'<ul '.$this->class_menu.'>'."\n";




    // asort($menu);
    usort($menu, function($a, $b) {
        return $a['orders'] - $b['orders'];
    });


    // loop through the $menu array() and build the parent menus.
    for ($i = 0; $i < count($menu); $i++)
    {
        if (is_array($menu[$i]))    // must be by construction but let's keep the errors home
        {
            if ($menu[$i]['show'] && $menu[$i]['parent'] == 0)    // are we allowed to see this menu?
            {
                if ($menu[$i]['is_parent'] == TRUE)
                {
                    // CodeIgniter's anchor(uri segments, text, attributes) tag.
                    $html_out .= "\t\t\t".'<li class="dropdown">'.anchor($menu[$i]['url'], ''.$menu[$i]['title'].'<span class="caret"></span>', 'class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false"');
                }
                else
                {
                    $html_out .= "\t\t\t\t".'<li>'.anchor($menu[$i]['url'], '<span>'.$menu[$i]['title'].'</span>');
                }

                // loop through and build all the child submenus.
                $html_out .= $this->get_childs($menu, $i);

                $html_out .= '</li>'."\n";
            }
        }
        else
        {
            exit (sprintf('menu nr %s must be an array', $i));
        }
    }

    $html_out .= "\t\t".'</ul>' . "\n";
    $html_out .= "\t".'</div>' . "\n";

    return $html_out;
}
/**
 * get_childs($menu, $parent_id) - SEE Above Method.
 *
 * Description:
 *
 * Builds all child submenus using a recurse method call.
 *
 * @param    mixed    $menu    array()
 * @param    string    $parent_id    id of parent calling this method.
 * @return    mixed    $html_out if has subcats else FALSE
 */
function get_childs($menu, $parent_id)
{
    $has_subcats = FALSE;

    $html_out  = '';
    $html_out .= "\t\t\t\t\t".'<ul class="dropdown-menu">'."\n";

    for ($i = 0; $i < count($menu); $i++)
    {
        if ($menu[$i]['show'] && $menu[$i]['parent'] == $parent_id)    // are we allowed to see this menu?
        {
            $has_subcats = TRUE;

            if ($menu[$i]['is_parent'] == TRUE)
            {
                $html_out .= "\t\t\t\t\t\t".'<li>'.anchor($menu[$i]['url'], '<span>'.$menu[$i]['title'].'</span>');
            }
            else
            {
                $html_out .= "\t\t\t\t\t\t".'<li>'.anchor($menu[$i]['url'], '<span>'.$menu[$i]['title'].'</span>');
            }

            // Recurse call to get more child submenus.
            $html_out .= $this->get_childs($menu, $i);

            $html_out .= '</li>' . "\n";
        }
    }
    $html_out .= "\t\t\t\t\t".'</ul>' . "\n";

    return ($has_subcats) ? $html_out : FALSE;
}

}

Print result:

Array ( [0] => Array ( [id] => 5 [title] => testurl [page] => 0 [url] => http://www.google.nl [orders] => 0 [parent] => 0 [is_parent] => 1 [show] => 1 ) [1] => Array ( [id] => 3 [title] => Home2 [page] => 1 [url] => [orders] => 0 [parent] => 0 [is_parent] => 0 [show] => 0 ) [2] => Array ( [id] => 2 [title] => Jantje [page] => 3 [url] => /home/jantje [orders] => 1 [parent] => 5 [is_parent] => 0 [show] => 1 ) [3] => Array ( [id] => 4 [title] => Jantje2 [page] => 3 [url] => [orders] => 1 [parent] => 3 [is_parent] => 0 [show] => 0 ) [4] => Array ( [id] => 1 [title] => Home [page] => 1 [url] => / [orders] => 2 [parent] => 0 [is_parent] => 1 [show] => 1 ) [5] => Array ( [id] => 6 [title] => Demo [page] => 2 [url] => /demo [orders] => 3 [parent] => 1 [is_parent] => 0 [show] => 1 ) )

Can somebody please help me to fix this?

Thanks

1 个答案:

答案 0 :(得分:1)

You're iterating from 1 to 6 when you should be iterating from 0 to 5. Change:

for ($i = 1; $i <= count($menu); $i++)

to:

for ($i = 0; $i < count($menu); $i++)