PHP无限分组

PHP高级技术群:424844502

世界上最好的语言不是吹的,轻松实现一个令人叹为观止的无限级别分组列表。

/**
 * 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, 'name' ) );

Array
(
    [0] => 学生1
    [1] => 学生4
    [2] => 学生2
    [3] => 学生3
    [4] => 学生5
)
var_dump ( dict ( $list, array (
		null
) ) );

array(5) {
  [0]=>
  NULL
  [1]=>
  NULL
  [2]=>
  NULL
  [3]=>
  NULL
  [4]=>
  NULL
}
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
        )

)
print_r ( dict ( $list, array (
		'name'
), 'school', 'id' ) );

Array
(
    [学校1] => Array
        (
            [1] => Array
                (
                    [name] => 学生1
                )

            [2] => Array
                (
                    [name] => 学生2
                )

        )

    [学校2] => Array
        (
            [4] => Array
                (
                    [name] => 学生4
                )

            [3] => Array
                (
                    [name] => 学生3
                )

            [5] => Array
                (
                    [name] => 学生5
                )

        )

)
print_r ( dict ( $list, array (
		'name'
), array (
		'school',
		'class'
), 'id' ) );

Array
(
    [学校1] => Array
        (
            [班级1] => Array
                (
                    [1] => Array
                        (
                            [name] => 学生1
                        )

                )

            [班级2] => Array
                (
                    [2] => Array
                        (
                            [name] => 学生2
                        )

                )

        )

    [学校2] => Array
        (
            [班级2] => Array
                (
                    [4] => Array
                        (
                            [name] => 学生4
                        )

                )

            [班级1] => Array
                (
                    [3] => Array
                        (
                            [name] => 学生3
                        )

                )

            [班级3] => Array
                (
                    [5] => Array
                        (
                            [name] => 学生5
                        )

                )

        )

)

 

发表评论

电子邮件地址不会被公开。

*