代码
<?php
/**
* 把二维表分解成多级数组,做菜单时常用
* (可用于无线分级菜单,无限级留言)
* @author Lonely (从以前BLOG转过来的)
* |----------------------|
* |id |parentId | value|
* |----------------------|
* parentId 存id的值,表示该项的父项是id为该项parentId的值的那个记录
* 如上表:parentId为父字段,id为子字段
*/
class unLimit {
/**
* 设置子字段
* @param string
*/
public static $subIdField='subId';
/**
* 设置父字段
* @param string
*/
public static $parentIdField='parentId';
/**
* 设子字段值的键值
* @param string
*/
public static $subField='sub';
/**
* 是否重新分配KEY
* @param boolean
*/
public static $reSortKey=false;
/**
* 处理分级数组并返回
* @param array $array
* @return array
*/
public static function toSub($array){
if(is_array($array)){
$proarr=array();
foreach($array as $row){
$proarr [$row [self::$parentIdField]] = $row;
$proarr [$row [self::$subIdField]] [self::$subField] [$row [self::$parentIdField]] = $row;
}
$proarr=self::search_sub($proarr,0);
if(self::$reSortKey){
$proarr=self::re_sort_key($proarr);
}
return $proarr;
}else
return $array;
}
/**
* 关键算法函数
* @param array $array
* @param string $key
* @return array
*/
private static function search_sub(array $array,$key){
$return = array ();
$subs = isset ( $array [$key] [self::$subField] ) ? $array [$key] [self::$subField] : array ();
foreach ( $subs as $k => $v ) {
$temp=$v;
$temp[self::$subField]=self::search_sub ( $array, $k );
$return [$k] = $temp;
}
return $return;
}
/**
* 重新排序KEY
* @param array $array
* @return array
*/
private static function re_sort_key($array){
$array=array_values($array);
foreach($array as $k=>$v){
if(is_array($v[self::$subField])&&!empty($v[self::$subField])){
$array[$k][self::$subField]=self::re_sort_key($v[self::$subField]);
}
}
return $array;
}
}
$temp=array (
0 =>
array (
'a' => '0',
'b' => '1',
'c' => 'a1',
),
1 =>
array (
'a' => '0',
'b' => '2',
'c' => 'a2',
),
2 =>
array (
'a' => '1',
'b' => '3',
'c' => 'a3',
),
3 =>
array (
'a' => '1',
'b' => '4',
'c' => 'a4',
),
4 =>
array (
'a' => '3',
'b' => '5',
'c' => 'a5',
),
5 =>
array (
'a' => '3',
'b' => '6',
'c' => 'a6',
),
6 =>
array (
'a' => '5',
'b' => '7',
'c' => 'a7',
),
7 =>
array (
'a' => '5',
'b' => '8',
'c' => 'a8',
),
8 =>
array (
'a' => '5',
'b' => '9',
'c' => 'a9',
),
9 =>
array (
'a' => '6',
'b' => '10',
'c' => 'a10',
),
10 =>
array (
'a' => '6',
'b' => '11',
'c' => 'a11',
),
11 =>
array (
'a' => '4',
'b' => '12',
'c' => 'a12',
),
12 =>
array (
'a' => '4',
'b' => '13',
'c' => 'a13',
),
13 =>
array (
'a' => '12',
'b' => '14',
'c' => 'a14',
),
14 =>
array (
'a' => '12',
'b' => '15',
'c' => 'a15',
),
15 =>
array (
'a' => '12',
'b' => '16',
'c' => 'a16',
),
16 =>
array (
'a' => '13',
'b' => '17',
'c' => 'a17',
),
17 =>
array (
'a' => '13',
'b' => '18',
'c' => 'a18',
),
18 =>
array (
'a' => '2',
'b' => '19',
'c' => 'a19',
),
19 =>
array (
'a' => '2',
'b' => '20',
'c' => 'a20',
),
20 =>
array (
'a' => '2',
'b' => '21',
'c' => 'a21',
),
21 =>
array (
'a' => '19',
'b' => '22',
'c' => 'a22',
),
22 =>
array (
'a' => '19',
'b' => '23',
'c' => 'a23',
),
23 =>
array (
'a' => '19',
'b' => '24',
'c' => 'a24',
),
24 =>
array (
'a' => '20',
'b' => '25',
'c' => 'a25',
),
25 =>
array (
'a' => '20',
'b' => '26',
'c' => 'a26',
),
26 =>
array (
'a' => '20',
'b' => '27',
'c' => 'a27',
),
27 =>
array (
'a' => '20',
'b' => '28',
'c' => 'a28',
),
28 =>
array (
'a' => '21',
'b' => '29',
'c' => 'a29',
),
29 =>
array (
'a' => '21',
'b' => '30',
'c' => 'a30',
),
30 =>
array (
'a' => '21',
'b' => '31',
'c' => 'a31',
),
31 =>
array (
'a' => '21',
'b' => '32',
'c' => 'a32',
),
);
unlimit::$subIdField="a";
unlimit::$parentIdField='b';
unlimit::$reSortKey=true;
$subs=unlimit::toSub($temp);
print_r($subs);
/**
* 把二维表分解成多级数组,做菜单时常用
* (可用于无线分级菜单,无限级留言)
* @author Lonely (从以前BLOG转过来的)
* |----------------------|
* |id |parentId | value|
* |----------------------|
* parentId 存id的值,表示该项的父项是id为该项parentId的值的那个记录
* 如上表:parentId为父字段,id为子字段
*/
class unLimit {
/**
* 设置子字段
* @param string
*/
public static $subIdField='subId';
/**
* 设置父字段
* @param string
*/
public static $parentIdField='parentId';
/**
* 设子字段值的键值
* @param string
*/
public static $subField='sub';
/**
* 是否重新分配KEY
* @param boolean
*/
public static $reSortKey=false;
/**
* 处理分级数组并返回
* @param array $array
* @return array
*/
public static function toSub($array){
if(is_array($array)){
$proarr=array();
foreach($array as $row){
$proarr [$row [self::$parentIdField]] = $row;
$proarr [$row [self::$subIdField]] [self::$subField] [$row [self::$parentIdField]] = $row;
}
$proarr=self::search_sub($proarr,0);
if(self::$reSortKey){
$proarr=self::re_sort_key($proarr);
}
return $proarr;
}else
return $array;
}
/**
* 关键算法函数
* @param array $array
* @param string $key
* @return array
*/
private static function search_sub(array $array,$key){
$return = array ();
$subs = isset ( $array [$key] [self::$subField] ) ? $array [$key] [self::$subField] : array ();
foreach ( $subs as $k => $v ) {
$temp=$v;
$temp[self::$subField]=self::search_sub ( $array, $k );
$return [$k] = $temp;
}
return $return;
}
/**
* 重新排序KEY
* @param array $array
* @return array
*/
private static function re_sort_key($array){
$array=array_values($array);
foreach($array as $k=>$v){
if(is_array($v[self::$subField])&&!empty($v[self::$subField])){
$array[$k][self::$subField]=self::re_sort_key($v[self::$subField]);
}
}
return $array;
}
}
$temp=array (
0 =>
array (
'a' => '0',
'b' => '1',
'c' => 'a1',
),
1 =>
array (
'a' => '0',
'b' => '2',
'c' => 'a2',
),
2 =>
array (
'a' => '1',
'b' => '3',
'c' => 'a3',
),
3 =>
array (
'a' => '1',
'b' => '4',
'c' => 'a4',
),
4 =>
array (
'a' => '3',
'b' => '5',
'c' => 'a5',
),
5 =>
array (
'a' => '3',
'b' => '6',
'c' => 'a6',
),
6 =>
array (
'a' => '5',
'b' => '7',
'c' => 'a7',
),
7 =>
array (
'a' => '5',
'b' => '8',
'c' => 'a8',
),
8 =>
array (
'a' => '5',
'b' => '9',
'c' => 'a9',
),
9 =>
array (
'a' => '6',
'b' => '10',
'c' => 'a10',
),
10 =>
array (
'a' => '6',
'b' => '11',
'c' => 'a11',
),
11 =>
array (
'a' => '4',
'b' => '12',
'c' => 'a12',
),
12 =>
array (
'a' => '4',
'b' => '13',
'c' => 'a13',
),
13 =>
array (
'a' => '12',
'b' => '14',
'c' => 'a14',
),
14 =>
array (
'a' => '12',
'b' => '15',
'c' => 'a15',
),
15 =>
array (
'a' => '12',
'b' => '16',
'c' => 'a16',
),
16 =>
array (
'a' => '13',
'b' => '17',
'c' => 'a17',
),
17 =>
array (
'a' => '13',
'b' => '18',
'c' => 'a18',
),
18 =>
array (
'a' => '2',
'b' => '19',
'c' => 'a19',
),
19 =>
array (
'a' => '2',
'b' => '20',
'c' => 'a20',
),
20 =>
array (
'a' => '2',
'b' => '21',
'c' => 'a21',
),
21 =>
array (
'a' => '19',
'b' => '22',
'c' => 'a22',
),
22 =>
array (
'a' => '19',
'b' => '23',
'c' => 'a23',
),
23 =>
array (
'a' => '19',
'b' => '24',
'c' => 'a24',
),
24 =>
array (
'a' => '20',
'b' => '25',
'c' => 'a25',
),
25 =>
array (
'a' => '20',
'b' => '26',
'c' => 'a26',
),
26 =>
array (
'a' => '20',
'b' => '27',
'c' => 'a27',
),
27 =>
array (
'a' => '20',
'b' => '28',
'c' => 'a28',
),
28 =>
array (
'a' => '21',
'b' => '29',
'c' => 'a29',
),
29 =>
array (
'a' => '21',
'b' => '30',
'c' => 'a30',
),
30 =>
array (
'a' => '21',
'b' => '31',
'c' => 'a31',
),
31 =>
array (
'a' => '21',
'b' => '32',
'c' => 'a32',
),
);
unlimit::$subIdField="a";
unlimit::$parentIdField='b';
unlimit::$reSortKey=true;
$subs=unlimit::toSub($temp);
print_r($subs);