# PHP无限分组
> 世界上最好的语言不是吹的,轻松实现一个令人叹为观止的无限级别分组列表。
~~~
/**
* group by any level
* http://blog.phpdr.net/php%E6%97%A0%E6%9E%81%E5%88%86%E7%BB%84.html
*
* @param array $list
* @param mixed $columns
* @param mixed $group
* @param mixed $primary
* @return array
*/
function dict(array $list, $columns = null, $group = null, $primary = null) {
if (! isset ( $primary )) {
$primary = array ();
}
if (is_string ( $primary )) {
$primary = array (
$primary
);
}
if (is_string ( $group )) {
$group = array (
$group
);
}
$listNew = array ();
foreach ( $list as $v ) {
$key = null;
foreach ( $primary as $v1 ) {
$key .= $v [$v1];
}
if (isset ( $columns )) {
if (is_array ( $columns ) && 1 === count ( $columns ) && $columns [0] === null) {
$vNew = null;
} else {
$vNew = array ();
if (is_array ( $columns )) {
foreach ( $columns as $k1 => $v1 ) {
if (is_int ( $k1 )) {
$k1 = $v1;
}
$vNew [$k1] = $v [$v1];
}
} else {
$vNew = $v [$columns];
}
}
} else {
$vNew = $v;
}
if (isset ( $group )) {
$vGroup = &$listNew;
foreach ( $group as $v2 ) {
if (isset ( $vGroup ) && array_key_exists ( $v [$v2], $vGroup )) {
$vGroup = &$vGroup [$v [$v2]];
} else {
$vGroup [$v [$v2]] = array ();
$vGroup = &$vGroup [$v [$v2]];
}
}
if (isset ( $key )) {
$vGroup [$key] = $vNew;
} else {
$vGroup [] = $vNew;
}
} else {
if (isset ( $key )) {
$listNew [$key] = $vNew;
} else {
$listNew [] = $vNew;
}
}
}
return $listNew;
}
$list = array ();
$list [] = array (
'id' => 1,
'name' => '学生1',
'school' => '学校1',
'class' => '班级1'
);
$list [] = array (
'id' => 4,
'name' => '学生4',
'school' => '学校2',
'class' => '班级2'
);
$list [] = array (
'id' => 2,
'name' => '学生2',
'school' => '学校1',
'class' => '班级2'
);
$list [] = array (
'id' => 3,
'name' => '学生3',
'school' => '学校2',
'class' => '班级1'
);
$list [] = array (
'id' => 5,
'name' => '学生5',
'school' => '学校2',
'class' => '班级3'
);
echo '<pre>';
print_r ( dict ( $list, 'name' ) );
var_dump ( dict ( $list, array (
null
) ) );
print_r ( dict ( $list, array (
'id',
'name',
'school'
), null, 'id' ) );
print_r ( dict ( $list, null, null, array (
'class',
'name'
) ) );
print_r ( dict ( $list, array (
'name'
), 'school', 'id' ) );
print_r ( dict ( $list, array (
'name'
), array (
'school',
'class'
), 'id' ) );
~~~
~~~
print_r ( dict ( $list, array (
'id',
'name',
'school'
), null, 'id' ) );
Array
(
[1] => Array
(
[id] => 1
[name] => 学生1
[school] => 学校1
)
[4] => Array
(
[id] => 4
[name] => 学生4
[school] => 学校2
)
[2] => Array
(
[id] => 2
[name] => 学生2
[school] => 学校1
)
[3] => Array
(
[id] => 3
[name] => 学生3
[school] => 学校2
)
[5] => Array
(
[id] => 5
[name] => 学生5
[school] => 学校2
)
)
~~~
~~~
print_r ( dict ( $list, null, null, array (
'class',
'name'
) ) );
Array
(
[班级1学生1] => Array
(
[id] => 1
[name] => 学生1
[school] => 学校1
[class] => 班级1
)
[班级2学生4] => Array
(
[id] => 4
[name] => 学生4
[school] => 学校2
[class] => 班级2
)
[班级2学生2] => Array
(
[id] => 2
[name] => 学生2
[school] => 学校1
[class] => 班级2
)
[班级1学生3] => Array
(
[id] => 3
[name] => 学生3
[school] => 学校2
[class] => 班级1
)
[班级3学生5] => Array
(
[id] => 5
[name] => 学生5
[school] => 学校2
[class] => 班级3
)
)
~~~
- 常用函数
- 简单的php生成静态html代码
- PHP写文件函数
- PHP生成GUID的函数
- PHP常用正则表达式汇总
- php字符串压缩
- PHP无限分组
- PHP简单 对象(object) 与 数组(array) 的转换
- PHP数组常用函数
- PHP调式测试函数
- PHP常用字符串的操作函数
- mysql 常用内置函数
- PHP通用请求函数CURL封装
- 裁剪图片PHP代码
- PDO操作MYSQL封装类
- 10个实用的PHP代码片段
- 获取访问者IP地址
- PHP实现发红包程序
- PHP把文本转换成图片
- curl重写php file_get_contents
- PHP生成一个随机字符串
- PHP读文件和写文件
- PHP根据key 给二维数组分组
- php中curl模拟post提交多维数组
- 33个超级有用必须要收藏的PHP代码样例
- PHP防XSS 防SQL注入的代码
- php常用数组array函数实例总结
- 用PHP遍历目录下的全部文件
- GBK2UTF8