Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-micro
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-micro
Commits
bc6ed1d7
Commit
bc6ed1d7
authored
Apr 07, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加树状结构相关方法
parent
7b4a8967
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
10 deletions
+50
-10
functions.php
src/functions.php
+50
-10
No files found.
src/functions.php
View file @
bc6ed1d7
...
@@ -2,8 +2,8 @@
...
@@ -2,8 +2,8 @@
use
Hyperf\HttpServer\Contract\RequestInterface
;
use
Hyperf\HttpServer\Contract\RequestInterface
;
use
Hyperf\HttpServer\Contract\ResponseInterface
;
use
Hyperf\HttpServer\Contract\ResponseInterface
;
use
Hyperf\Utils\ApplicationContext
;
use
Hyperf\Redis\Redis
;
use
Hyperf\Redis\Redis
;
use
Hyperf\Utils\ApplicationContext
;
/**
/**
* 容器实例
* 容器实例
...
@@ -288,24 +288,64 @@ if (!function_exists('now')) {
...
@@ -288,24 +288,64 @@ if (!function_exists('now')) {
if
(
!
function_exists
(
'get_tree_id'
))
{
if
(
!
function_exists
(
'get_tree_id'
))
{
/**
/**
* @param $array
* @param array $array
* @param int $pid
* @param array $pid
* @param int $level
* @return array
* @return array
*/
*/
function
get_tree_id
(
$array
,
$pid
=
0
,
$level
=
0
)
function
get_tree_id
(
array
$array
,
$pids
=
[
0
]
)
{
{
static
$list
=
[];
$list
=
[];
foreach
(
$array
as
$key
=>
$value
)
{
foreach
(
$array
as
$key
=>
$value
)
{
if
(
$value
[
'pid'
]
==
$pid
||
$value
[
'id'
]
==
$pid
)
{
if
(
in_array
(
$value
[
'pid'
],
$pids
)
||
in_array
(
$value
[
'id'
],
$pids
))
{
$value
[
'level'
]
=
$level
;
$list
[]
=
$value
[
'id'
];
$list
[]
=
$value
[
'id'
];
unset
(
$array
[
$key
]);
unset
(
$array
[
$key
]);
get_tree_id
(
$array
,
$value
[
'id'
],
$level
+
1
);
}
}
}
}
return
$list
;
if
(
$list
==
[])
return
[];
$children
=
get_tree_id
(
$array
,
$list
);
return
array_merge
(
$list
,
$children
);
}
}
}
}
if
(
!
function_exists
(
'get_tree'
))
{
/**
* 树状的算法
* @param array $array 代转化数组
* @param int $pid 起始节点
* @return array
*/
function
get_tree
(
$array
=
[],
$pid
=
0
)
{
$list
=
[];
// 获取每个节点的直属子节点,*记住是直属,不是所有子节点
foreach
(
$array
as
$item
)
{
if
(
isset
(
$list
[
$item
[
'pid'
]]))
{
$list
[
$item
[
'pid'
]][]
=
$item
;
}
else
{
$list
[
$item
[
'pid'
]]
=
[
$item
];
}
}
return
format_tree
(
$list
);
}
}
if
(
!
function_exists
(
'format_tree'
))
{
/**
* 利用递归格式化每个节点
* @param array $array 代转化数组
* @param int $pid 起始节点
* @return array
*/
function
format_tree
(
array
$array
=
[],
$pid
=
0
)
{
$result
=
[];
if
(
!
isset
(
$array
[
$pid
]))
{
return
$result
;
}
foreach
(
$array
[
$pid
]
as
$item
)
{
$item
[
'children'
]
=
format_tree
(
$array
,
$item
[
'id'
]);
array_push
(
$result
,
$item
);
}
return
$result
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment