From bf61081911f83bda5363e880270ece8ea8fec2a0 Mon Sep 17 00:00:00 2001 From: fengyuexingzi Date: Thu, 26 Oct 2017 01:38:38 +0800 Subject: [PATCH] true tree --- tree.php | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/tree.php b/tree.php index 78803aa..3c5e2e0 100644 --- a/tree.php +++ b/tree.php @@ -12,21 +12,29 @@ $test_data = [ ['id' => 1, 'name' => 'a', 'pid' => 0], ['id' => 2, 'name' => 'b', 'pid' => 1], ['id' => 3, 'name' => 'c', 'pid' => 1], - ['id' => 4, 'name' => 'd', 'pid' => 0], - ['id' => 5, 'name' => 'e', 'pid' => 4], + ['id' => 4, 'name' => 'd', 'pid' => 5], + ['id' => 5, 'name' => 'e', 'pid' => 0], + ['id' => 6, 'name' => 'e', 'pid' => 4], ]; function m_tree($arr, $pid = 0) { $tree = []; - foreach ($arr as $tmp) { - if (!isset($tmp['pid'])) { - $tree[] = $tmp; - } elseif($tmp['pid'] == $pid){ - $tree[$pid][$tmp['id']] = $tmp; - } /*else { - if ($tmp['pid'] > $pid) - $tree[$pid][] = m_tree($arr, $tmp['pid']); - }*/ + foreach ($arr as $t) { + if ($t['id'] == $pid) { + $tree = $t; + } + } + foreach ($arr as $child) { + if (isset($child['pid']) && $child['pid'] == $tree['id']) { + $tree['child'][] = $child; + } + } + foreach ($tree as $k1 => $v1) { + if (is_array($v1)) { + foreach ($v1 as $k2 => $v2) { + $tree['child'][$k2] = m_tree($arr, $v2['id']); + } + } } return $tree; }