{"id":372,"date":"2018-07-03T17:14:02","date_gmt":"2018-07-03T09:14:02","guid":{"rendered":"http:\/\/blog.inforere.com\/?p=372"},"modified":"2022-06-16T17:47:02","modified_gmt":"2022-06-16T09:47:02","slug":"%e4%bb%8e%e8%a1%a8%e6%a0%bc%e6%95%b0%e6%8d%ae%e6%9e%84%e5%bb%ba%e6%a0%91%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/blog.inforere.com\/?p=372","title":{"rendered":"\u4ece\u8868\u683c\u6570\u636e\u6784\u5efa\u6811\u7ed3\u6784"},"content":{"rendered":"\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-1 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\" style=\"flex-basis:100%\">\n<p>\u7279\u70b9:<\/p>\n<ol>\n<li>\u4e00\u6b21\u52a0\u8f7d\u6240\u6709\u6570\u636e array(id, pid), array(id, pid)<\/li>\n<li>\u901a\u8fc7\u4e00\u6b21\u5faa\u73af\u6784\u5efa\u6811\u578b\u7ed3\u6784<\/li>\n<\/ol>\n<p><a href=\"http:\/\/blog.inforere.com\/wp-content\/uploads\/2018\/07\/\u5fae\u4fe1\u56fe\u7247_20180703170915.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-373\" src=\"http:\/\/blog.inforere.com\/wp-content\/uploads\/2018\/07\/\u5fae\u4fe1\u56fe\u7247_20180703170915-768x1024.jpg\" alt=\"\" width=\"576\" height=\"768\" srcset=\"https:\/\/blog.inforere.com\/wp-content\/uploads\/2018\/07\/\u5fae\u4fe1\u56fe\u7247_20180703170915-768x1024.jpg 768w, https:\/\/blog.inforere.com\/wp-content\/uploads\/2018\/07\/\u5fae\u4fe1\u56fe\u7247_20180703170915-225x300.jpg 225w, https:\/\/blog.inforere.com\/wp-content\/uploads\/2018\/07\/\u5fae\u4fe1\u56fe\u7247_20180703170915.jpg 1080w\" sizes=\"(max-width: 576px) 100vw, 576px\" \/><\/a><\/p>\n<pre class=\"lang:default decode:true \">\/**\n * Class BTree\n * @package j\\tree\\tests\n * @property array $tree\n *\/\nclass BTree {\n\n    protected $source;\n    public $index = [];\n\n    public $idKey = 'id';\n    public $pidKey = 'pid';\n    public $childKey = 'child';\n\n    \/**\n     * BTree constructor.\n     * @param $source\n     *\/\n    public function __construct($source){\n        $this-&gt;source = $source;\n    }\n\n    public function __get($name){\n        if($name == 'tree'){\n            return $this-&gt;tree = $this-&gt;buildTree();\n        } else {\n            return null;\n        }\n    }\n\n    protected function buildTree(){\n        $keyId = $this-&gt;idKey;\n        $keyPid = $this-&gt;pidKey;\n        $keyChild = $this-&gt;childKey;\n\n        $tree = array();\n        $childMap =&amp; $this-&gt;index;\n        foreach($this-&gt;source as $item) {\n            $item[$keyChild] = array(); \/\/\u7ed9\u6bcf\u4e2a\u8282\u70b9\u9644\u52a0\u4e00\u4e2achild\u9879\n            $key = $item[$keyId];\n\n            if($item[$keyPid] == 0) {\n                if(isset($childMap[$key])){\n                    \/\/ \u5df2\u8bbe\u7f6e\u5f53\u524d\u5b50\u7c7b\u6620\u5c04\n                    $childMap[$key] += $item;\n                    $tree[$key] =&amp; $childMap[$key];\n                } else {\n                    $tree[$key] = $item;\n                    $childMap[$key] =&amp; $tree[$key];\n                }\n            }else {\n                $pid = $item[$keyPid];\n                if(isset($childMap[$key])){\n                    \/\/ \u5df2\u8bbe\u7f6e\u5f53\u524d\u5b50\u7c7b\u6620\u5c04\n                    $childMap[$key] += $item;\n                    $childMap[$pid][$keyChild][$key] =&amp; $childMap[$key];\n                } else {\n                    $childMap[$pid][$keyChild][$key] = $item;\n                    $childMap[$key] =&amp; $childMap[$pid][$keyChild][$key];\n                }\n            }\n        }\n        return $tree;\n    }\n\n    function getTree(){\n        return $this-&gt;tree;\n    }\n\n    \/**\n     * @param $id\n     * @return array\n     *\/\n    function getParent($id){\n        return $this-&gt;index[$id];\n    }\n\n    function getParents($id, $self = false, $maxLevel = 6) {\n        $keyId = $this-&gt;idKey;\n        $keyPid = $this-&gt;pidKey;\n\n        $item = $this-&gt;index[$id];\n        $parents = $self ? [$id =&gt; $item] : [];\n        $i = 0;\n        while(isset($this-&gt;index[$item[$keyPid]])){\n            $item = $parents[$item[$keyId]] =  $this-&gt;index[$item[$keyPid]];\n            if($i++ &gt; $maxLevel){\n                break;\n            }\n        }\n        return array_reverse($parents, true);\n    }\n\n    function getParentIds($id, $self = false, $maxLevel = 6){\n        return array_keys($this-&gt;getParents($id, $self, $maxLevel));\n    }\n\n    function parentId($id){\n        return $this-&gt;index[$id][$this-&gt;pidKey];\n    }\n\n    \/**\n     * @param $id\n     * @return array\n     *\/\n    function getChild($id){\n        return $this-&gt;index[$id][$this-&gt;childKey];\n    }\n\n    function getChildIdentifying($id, $self = false, $recursive = true){\n        $ids = $self ? [$id] : array();\n        $items = $this-&gt;index[$id][$this-&gt;childKey];\n        if($recursive){\n            while($item = array_pop($items)){\n                $ids[] = $item[$this-&gt;idKey];\n                if(count($item[$this-&gt;childKey]) &gt; 0){\n                    foreach($item[$this-&gt;childKey] as $_item){\n                        array_push($items, $_item);\n                    }\n                }\n            }\n        } else {\n            $ids = array_merge($ids, array_keys($items[$this-&gt;childKey]));\n        }\n        return $ids;\n\n\/\/        array_walk_recursive($this-&gt;tree[$id]['child'], function($item, $key) use(&amp; $ids){\n\/\/            if($key == 'id'){\n\/\/                $ids[] = $item;\n\/\/            }\n\/\/        });\n    }\n};\n\n\/\/ b+\u6811\n\/\/ \u6d4b\u8bd5\u6570\u636e\n$data = array(\n    array('id' =&gt; 1, 'pid' =&gt; 0),\n    array('id' =&gt; 4, 'pid' =&gt; 2),\n    array('id' =&gt; 3, 'pid' =&gt; 2),\n    array('id' =&gt; 2, 'pid' =&gt; 1),\n);\n\n$t = new BTree($data);\n$tree = $t-&gt;getTree();\n\nassert_expr(count($tree) == 1, 'ROOT NODE TEST');\nassert_expr(count($tree[1]['child']) == 1, 'Child test1');\nassert_expr(count($tree[1]['child'][2]['child']) == 2, 'Child test1');\nassert_expr(count($t-&gt;getChildIdentifying(1)) == 3, 'Child ids');\nassert_expr(count($t-&gt;getChildIdentifying(2)) == 2, 'Child ids');\nassert_expr(count($t-&gt;getChildIdentifying(3)) == 0, 'Child ids');\nassert_expr(count($t-&gt;getChildIdentifying(4)) == 0, 'Child ids');<\/pre>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u7279\u70b9: \u4e00\u6b21\u52a0\u8f7d\u6240\u6709\u6570\u636e array(id, pid), array(id, pid) \u901a\u8fc7\u4e00\u6b21\u5faa\u73af\u6784\u5efa\u6811\u578b\u7ed3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/372"}],"collection":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=372"}],"version-history":[{"count":3,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/372\/revisions"}],"predecessor-version":[{"id":394,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/372\/revisions\/394"}],"wp:attachment":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}