modified 格式化代码

This commit is contained in:
zhaoxiang 2020-06-10 15:20:53 +08:00
parent 819d958c5b
commit 76568c334b

View File

@ -101,14 +101,15 @@ class Tools {
* 将二维数组变成指定key * 将二维数组变成指定key
* @param $array * @param $array
* @param $keyName * @param $keyName
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array * @return array
* @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public static function buildArrByNewKey($array, $keyName = 'id') { public static function buildArrByNewKey($array, $keyName = 'id') {
$list = array(); $list = array();
foreach ($array as $item) { foreach ($array as $item) {
$list[$item[$keyName]] = $item; $list[$item[$keyName]] = $item;
} }
return $list; return $list;
} }
@ -121,18 +122,18 @@ class Tools {
* @param string $root * @param string $root
* @return array * @return array
*/ */
public static function listToTree($list, $pk='id', $pid = 'fid', $child = 'children', $root = '0') { public static function listToTree($list, $pk = 'id', $pid = 'fid', $child = 'children', $root = '0') {
$tree = array(); $tree = array();
if(is_array($list)) { if (is_array($list)) {
$refer = array(); $refer = array();
foreach ($list as $key => $data) { foreach ($list as $key => $data) {
$refer[$data[$pk]] = &$list[$key]; $refer[$data[$pk]] = &$list[$key];
} }
foreach ($list as $key => $data) { foreach ($list as $key => $data) {
$parentId = $data[$pid]; $parentId = $data[$pid];
if ($root == $parentId) { if ($root == $parentId) {
$tree[] = &$list[$key]; $tree[] = &$list[$key];
}else{ } else {
if (isset($refer[$parentId])) { if (isset($refer[$parentId])) {
$parent = &$refer[$parentId]; $parent = &$refer[$parentId];
$parent[$child][] = &$list[$key]; $parent[$child][] = &$list[$key];
@ -140,29 +141,31 @@ class Tools {
} }
} }
} }
return $tree; return $tree;
} }
public static function formatTree($list, $lv = 0, $title = 'title'){ public static function formatTree($list, $lv = 0, $title = 'title') {
$formatTree = array(); $formatTree = array();
foreach($list as $key => $val){ foreach ($list as $key => $val) {
$title_prefix = ''; $title_prefix = '';
for( $i=0;$i<$lv;$i++ ){ for ($i = 0; $i < $lv; $i++) {
$title_prefix .= "|---"; $title_prefix .= "|---";
} }
$val['lv'] = $lv; $val['lv'] = $lv;
$val['namePrefix'] = $lv == 0 ? '' : $title_prefix; $val['namePrefix'] = $lv == 0 ? '' : $title_prefix;
$val['showName'] = $lv == 0 ? $val[$title] : $title_prefix.$val[$title]; $val['showName'] = $lv == 0 ? $val[$title] : $title_prefix . $val[$title];
if(!array_key_exists('children', $val)){ if (!array_key_exists('children', $val)) {
array_push($formatTree, $val); array_push($formatTree, $val);
}else{ } else {
$child = $val['children']; $child = $val['children'];
unset($val['children']); unset($val['children']);
array_push($formatTree, $val); array_push($formatTree, $val);
$middle = self::formatTree($child, $lv+1, $title); //进行下一层递归 $middle = self::formatTree($child, $lv + 1, $title); //进行下一层递归
$formatTree = array_merge($formatTree, $middle); $formatTree = array_merge($formatTree, $middle);
} }
} }
return $formatTree; return $formatTree;
} }
} }