Skip to content

Commit 826637c

Browse files
author
jiangbj
committed
优化
1 parent 263c1c4 commit 826637c

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

common_helper.php

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,42 +1042,46 @@ function get_full_month_time($start_time, $end_time)
10421042
}
10431043
}
10441044

1045-
/**
1046-
* 无限极非递归生成树
1047-
*
1048-
* @param array $items 例如:[['id' => 1, 'pid' => 0, 'name' => 'AA'],['id' => 2, 'pid' => 1, 'name' => 'BB']]
1049-
* @return array
1050-
*/
1051-
function generateTree($items)
1052-
{
1053-
$tree = [];
1054-
foreach ($items as $item) {
1055-
if (isset($items[$item['pid']])) {
1056-
$items[$item['pid']]['son'][] = &$items[$item['id']];
1057-
} else {
1058-
$tree[] = &$items[$item['id']];
1045+
if (!function_exists('generate_tree')) {
1046+
/**
1047+
* 无限极非递归生成树
1048+
*
1049+
* @param array $items 例如:[1=>['id' => 1, 'pid' => 0, 'name' => 'AA'],2=>['id' => 2, 'pid' => 1, 'name' => 'BB']] key值必须和id值保持一致
1050+
* @return array
1051+
*/
1052+
function generate_tree($items)
1053+
{
1054+
$tree = [];
1055+
foreach ($items as $item) {
1056+
if (isset($items[$item['pid']])) {
1057+
$items[$item['pid']]['son'][] = &$items[$item['id']];
1058+
} else {
1059+
$tree[] = &$items[$item['id']];
1060+
}
10591061
}
1060-
}
10611062

1062-
return $tree;
1063+
return $tree;
1064+
}
10631065
}
10641066

1065-
/**
1066-
* 动态添加数据
1067-
*
1068-
* @param array $items 例如:[['id' => 1, 'pid' => 0, 'name' => 'AA'],['id' => 2, 'pid' => 1, 'name' => 'BB']]
1069-
* @param int $pid
1070-
* @param string $name
1071-
*/
1072-
function addTree(&$items, $pid, $name)
1073-
{
1074-
$num = count($items) + 1;
1075-
if ($num == 0) {
1076-
$num = 0;
1067+
if (!function_exists('add_tree')) {
1068+
/**
1069+
* 动态添加数据
1070+
*
1071+
* @param array $items 例如:[1=>['id' => 1, 'pid' => 0, 'name' => 'AA'],2=>['id' => 2, 'pid' => 1, 'name' => 'BB']] key值必须和id值保持一致
1072+
* @param int $pid
1073+
* @param string $name
1074+
*/
1075+
function add_tree(&$items, $pid, $name)
1076+
{
1077+
$num = count($items) + 1;
1078+
if ($num == 0) {
1079+
$num = 0;
1080+
}
1081+
$items[$num] = [
1082+
'id' => $num,
1083+
'pid' => $pid,
1084+
'name' => $name,
1085+
];
10771086
}
1078-
$items[$num] = [
1079-
'id' => $num,
1080-
'pid' => $pid,
1081-
'name' => $name,
1082-
];
1083-
}
1087+
}

0 commit comments

Comments
 (0)