php排序数组给出:未定义的偏移量:6

米奇·丹尼尔斯(Mitch Daniels)

我试图按顺序对数组进行排序,但是当我使用sort或sort array等时,它给了我未定义的偏移量错误。如果不进行排序,它就完美了!

脚本:

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

}

打印结果:

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 ) )

有人可以帮我解决这个问题吗?

谢谢

保罗

您应该从1迭代到6,而应该从0迭代到5。更改:

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

到:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PHP-未定义偏移量:0

来自分类Dev

PHP:for循环中的未定义偏移量

来自分类Dev

PHP从list()的未定义偏移量

来自分类Dev

未定义的偏移量:6

来自分类Dev

PHP注意:未定义的偏移量错误

来自分类Dev

PHP未定义的偏移量数组

来自分类Dev

获取未定义的偏移量:0 php数组

来自分类Dev

修复随机php数组-未定义的偏移量:

来自分类Dev

PHP中的未定义偏移量错误3

来自分类Dev

注意:未定义的偏移量:3 php sql

来自分类Dev

PHP从list()的未定义偏移量

来自分类Dev

注意:未定义的偏移量:4,在PHP中使用数组遍历

来自分类Dev

注意:未定义的偏移量

来自分类Dev

未定义的偏移量:2(php函数)

来自分类Dev

'未定义的偏移量错误'PHP多维数组

来自分类Dev

PHP注意:未定义的偏移量

来自分类Dev

PHP循环中未定义的偏移量错误

来自分类Dev

数组索引中的未定义偏移量错误

来自分类Dev

PHP中未定义的偏移量0错误

来自分类Dev

数组中未定义的偏移量错误

来自分类Dev

“未定义的偏移量”,错误在哪里?(数组,PHP)

来自分类Dev

PHP错误:注意:未定义的偏移量

来自分类Dev

通过网络服务获取数组时PHP中的未定义偏移量

来自分类Dev

数组未定义偏移量与php

来自分类Dev

PHP 数组问题 - 未定义的偏移量

来自分类Dev

PHP list() 错误:“未定义的偏移量”

来自分类Dev

在 PHP 中打印多维数组时未定义的偏移量

来自分类Dev

PHP 注意:未定义的偏移量:1 - 将数组列转置为行

来自分类Dev

php获取总和数组,但有“通知”未定义的偏移量:0