hxari/tree

使用数组创建树结构。

v1.0.3 2022-10-19 14:00 UTC

This package is auto-updated.

Last update: 2024-09-11 00:52:58 UTC


README

Tree Avatar

关于

使用数组或对象创建树结构。你想在命令行界面创建类似的结构吗?没有问题!... Tree 是解决方案!.. 它真的非常简单,无需麻烦!

除了PHP之外,Tree还针对JavaScript版本进行了构建,但现在它只支持HTML显示,在下一个版本中,我可能会添加一个功能,使得Tree的JavaScript版本可以与NodeJS一起在命令行界面运行。

安装

使用Composer安装

composer require hxari/tree

使用Wget安装

wget https://raw.githubusercontent.com/hxAri/Tree/main/src/PHPTree/Tree.php

使用Git安装

git clone https://github.com/hxAri/Tree

或者,如果您不想麻烦,也可以直接从Github下载。

用法

非常简单的用法。

use Tree\Tree;

// If you use Composer.
require "vendor/autoload.php";

// If you use Git/ Download.
require "Tree/Tree.php";

// If you use Wget.
require "Tree.php";

// Suppose you have an array.
$array = [
    'users' => [
        [
            'id' => 2288,
            'profile' => [
                'fname' => "Xyz",
                'uname' => "xyz"
            ]
        ],
        [
            'id' => 2289,
            'profile' => [
                'fname' => "Xxx",
                'uname' => "xxx"
            ]
        ]
    ],
    'malware' => [
        'trojan' => "Horse"
    ]
];

// Creating a tree structure.
echo Tree::create( $array );

这将显示。

├── users
│   ├── Array
│   │   ├── id
│   │   │   └── 2288
│   │   └── profile
│   │       ├── fname
│   │       │   └── Xyz
│   │       └── uname
│   │           └── xyz
│   └── Array
│       ├── id
│       │   └── 2289
│       └── profile
│           ├── fname
│           │   └── Xxx
│           └── uname
│               └── xxx
└── malware
    └── trojan
        └── Horse

方法

方法说明。

::create 方法有三个参数:

Tree::create( Array $data, Int $start = 0, Int $flags = 0 )

参数 $data 是用于创建树结构的数组数据。

$start 参数用于标识起始处将使用多少空格。

同时,$flags 参数用于标识是否使用线条或点来创建结构。

::setKeyHandler 方法有一个参数,类型为 Callable。当您想要着色数组元素的键值时,这非常有用。

// Method.
Tree::setKeyHandler( Callable $handler );

// Method Use.
Tree::setKeyHandler( fn( Mixed $key ) => "\e[1;32m{$key}" );

// Or
Tree::setKeyHandler( function( Mixed $key ) {
    // Something....
});

::setValHandler 方法也有相同的功能。

常量

许可证

所有源代码均受 GNU通用公共许可证 保护。