php排序数组给出:未定义的偏移量:6
我试图按顺序排序数组,但当我使用sort或sort array等时,它会给我一个未定义的偏移量错误。没有分类,它的工作完美 脚本:php排序数组给出:未定义的偏移量:6,php,arrays,sorting,undefined,Php,Arrays,Sorting,Undefined,我试图按顺序排序数组,但当我使用sort或sort array等时,它会给我一个未定义的偏移量错误。没有分类,它的工作完美 脚本: class Dynamic_menu { private $ci; // for CodeIgniter Super Global Reference. private $id_menu = 'class="collapse navbar-collapse" id="bs-example-navbar-colla
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;
}
有人能帮我修一下吗
谢谢您正在从1迭代到6,而您应该从0迭代到5。更改:
for ($i = 1; $i <= count($menu); $i++)
对于($i=1;$i@MitchDaniels)您替换了,如果我用相同的内容更改了childs的第二部分,那么现在会出现500个错误。脚本头过早结束:index.php这都是因为usort。@MitchDaniels我认为这不是由usort
引起的。服务器错误日志中有什么内容吗?【2016年4月4日星期一20:59:18】【错误】mod_fcgid:process/var/www/cgi-bin/cgi_-wrapper/cgi_-wrapper(56749)退出(通信错误),仅当我尝试在php7上运行网站时,才会收到意外信号11。这是经过一些更改后的下一个错误。已将apache和php allready中的所有内容增加到最大值,但仍然存在此错误。致命错误:允许内存大小536870912字节已用尽(尝试分配523800字节)在第244Solution found!行的/var/www/html/system/core/Config.php中,确实有一个空变量。
for ($i = 1; $i <= count($menu); $i++)
for ($i = 0; $i < count($menu); $i++)