【问题标题】:php sorting array gives: Undefined offset: 6php排序数组给出:未定义的偏移量:6
【发布时间】:2016-04-04 18:18:42
【问题描述】:

我正在尝试对订单中的数组进行排序,但是当我使用排序或排序数组等时,它会给我一个未定义的偏移错误。无需排序即可完美运行!

脚本:

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

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

谢谢

【问题讨论】:

  • 这是一些更改后的下一个错误。已将 apache 和 php 中的所有内容增加到最大,但仍然出现此错误。致命错误:第 244 行 /var/www/html/system/core/Config.php 中允许的内存大小为 536870912 字节已用尽(尝试分配 523800 字节)
  • 找到解决方案!某处确实有一个空变量。

标签: php arrays sorting undefined


【解决方案1】:

当你应该从 0 迭代到 5 时,你正在从 1 迭代到 6。更改:

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

到:

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

【讨论】:

  • @MitchDaniels 你用&lt;替换了&lt;=
  • 如果我用相同的方式为孩子更改第二部分,那么它现在会给出 500 错误。脚本头的过早结束:index.php 这都是因为 usort。
  • @MitchDaniels 我不认为这是由usort 引起的。服务器错误日志中是否有任何内容?
  • [Mon Apr 04 20:59:18 2016] [error] mod_fcgid: 进程 /var/www/cgi-bin/cgi_wrapper/cgi_wrapper(56749) 退出(通信错误),得到意外信号 11
  • 只有当我尝试在 php7 上运行网站时它才能工作!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-23
  • 2014-10-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多