PHP code example of cvoid / easytree

1. Go to this page and download the library: Download cvoid/easytree library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cvoid / easytree example snippets




= [
    ['id' => 1, 'name' => '食物', 'parent_id' => 0],
    ['id' => 4, 'name' => '饮料', 'parent_id' => 1],
    ['id' => 5, 'name' => '矿泉水', 'parent_id' => 4],
];

$tree = (new \EasyTree\Tree\TreeBuilder($foo))
    ->setIdKey('id')
    ->setParentKey('parent_id')
    ->setChildrenKey('children')
    ->build();

print_r($tree->toArray());

echo -----
Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 食物
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 4
                            [name] => 饮料
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 5
                                            [name] => 矿泉水
                                        )

                                )

                        )

                )

        )

)



	$tree = (new \EasyTree\Tree\TreeBuilder($foo))
    	->setIdKey('id')
    	->setParentKey('parent_id')
    	->setChildrenKey('children')
    	->build();

	print_r($tree->toJson());

echo -----
[{"id":1,"name":"食物","parent_id":0,"children":[{"id":3,"name":"面条","parent_id":1},{"id":4,"name":"饮料","parent_id":1,"children":[{"id":5,"name":"矿泉水","parent_id":4}]}]},{"id":2,"name":"车辆","parent_id":0,"children":[{"id":6,"name":"校车","parent_id":2}]}]


$tree = (new \EasyTree\Tree\TreeBuilder($foo))
	    ->setIdKey('id')
	    ->setParentKey('parent_id')
	    ->setChildrenKey('children')
	    ->build();

$tree->each(
   function($node) {
	   # dump node data
		var_dump($node);
	}
)


  
	$tree = (new \EasyTree\Tree\TreeBuilder($foo))
    ->setIdKey('id')
    ->setParentKey('parent_id')
    ->setChildrenKey('children')
    ->build();
	
	#搜索节点id = 5的节点
	$node = $tree->search(
	function($node) {
		return $node->getId() === 5;
	}
);
	
  #搜索节点name = 矿泉水的节点
	$node = $tree->search(
	function($node) {
		return $node->getData()->toArray()['name'] === '矿泉水';
	}
);

	##你可以获取节点的高度 或者 数据
	$node->getData()->toArray();
  echo -----
    Array
		(
    		[id] => 5
    		[name] => 矿泉水
		)
    
  $node->getLevel();  
	echo -----
    3


$tree = (new \EasyTree\Tree\TreeBuilder($foo))
    ->setIdKey('id')
    ->setParentKey('parent_id')
    ->setChildrenKey('children')
    ->build();
 
$nodes = $tree->searchAll(
   function($node) {
     return $node->getParentId() === 1;
   }
);
 
print_r($nodes);
#这里会返回 [矿泉水TreeNode对象, 饮料TreeNode对象, 食品TreeNode对象]


  $tree = (new \EasyTree\Tree\TreeBuilder($foo))
      ->setIdKey('id')
      ->setParentKey('parent_id')
      ->setChildrenKey('children')
      ->build();

  var_dump(
      $tree->contain(function (\EasyTree\Tree\Node $node): bool {
          return $node->getData()['name'] === 1;
      })
  );
	
  var_dump(
      $tree->contain(function (\EasyTree\Tree\Node $node): bool {
          return $node->getData()['name'] === '校车';
      })
  );

echo ----
bool(false)
bool(true)


  $tree = (new \EasyTree\Tree\TreeBuilder($foo))
      ->setIdKey('id')
      ->setParentKey('parent_id')
      ->setChildrenKey('children')
      ->build();

  var_dump(
      $tree->isOverLevel(3)
  );

  var_dump(
      $tree->isOverLevel(4)
  );

echo ----
bool(false)
bool(true)