🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 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 ) ) ~~~