DROP TABLE IF EXISTS `menu0910`;
CREATE TABLE `menu0910` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`menu` varchar(50) COLLATE utf8_unicode_ci DEFAULT '',
`parentid` bigint(20) DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=40 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; -- ----------------------------
-- Records of menu0910
-- ----------------------------
INSERT INTO `menu0910` VALUES ('', '1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-1-3-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-2-3-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-1-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-2-3', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3-1', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3-2', '');
INSERT INTO `menu0910` VALUES ('', '1-3-3-3', '');
<?php
$mysqli = new mysqli('localhost', 'root', '', 'w'); if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
} $menulist = array();
/* Select queries return a resultset */
if ($result = $mysqli->query("SELECT * FROM menu0910")) {
printf("Select returned %d rows.\n", $result->num_rows); /* free result set */ while ($row = $result->fetch_assoc()) {
$menulist[] = $row;
} $result->close();
} var_dump($menulist); //筛选出3级idlist->2级->1级
$idlist_3 = array();
$idlist_2 = array();
$idlist_1 = array();
$idlist_1_2_3 = array();
$idlist_1_2 = array();
foreach($menulist as $menu)
{
$idlist_1_2_3[] = $menu['id'];
}
var_dump($idlist_1_2_3);
foreach($menulist as $menu)
{
if($menu['parentid']!=0)$idlist_1_2[] = $menu['parentid'];
}
$idlist_1_2=array_unique($idlist_1_2);
var_dump($idlist_1_2);
foreach($idlist_1_2_3 as $id)
{
if(!in_array($id,$idlist_1_2)) $idlist_3[] = $id;
}
var_dump($idlist_3);
foreach($idlist_3 as $id)
{
$idlist_2[] = get_parentid($id);
}
$idlist_2=array_unique($idlist_2);
var_dump($idlist_2); foreach($idlist_2 as $id)
{
$idlist_1[] = get_parentid($id);
}
$idlist_1=array_unique($idlist_1);
var_dump($idlist_1); function get_parentid($id)
{
global $menulist;
foreach($menulist as $one)
{
if($one['id']==$id) RETURN $one['parentid'];
}
} $res = array();
foreach($idlist_1 as $one)
{
$res[] = $one;
}
var_dump($res); $w = 0;
foreach($res as $id1)
{
$tmp = array();
foreach($idlist_2 as $id2)
{
if(get_parentid($id2) == $id1)$tmp[]=$id2;
}
$res[]=$tmp;
}
var_dump($res);
05-11 18:30