vhood/tree-converter

树结构类型切换器

1.1.0 2022-12-30 08:48 UTC

This package is auto-updated.

Last update: 2024-09-14 10:57:57 UTC


README

tests version downloads license

该包基于原生PHP数组,允许您切换树类型。

支持类型

  • 邻接表
  • 物化路径
  • 嵌套集
  • 关联数组树

查看 数据示例

安装

composer require vhood/tree-converter

要求

  • php >=5.6

用法

用法示例

use Vhood\TreeType\Converter;
use Vhood\TreeType\Type\AdjacencyList;

$adjacencyList = [
    [
        'id' => 1,
        'name' => 'node1',
        'parent_id' => null,
    ],
    [
        'id' => 2,
        'name' => 'node2',
        'parent_id' => 1,
    ],
];

$adjacencyListConverter = new Converter(new AdjacencyList($adjacencyList));

print_r($adjacencyListConverter->toAssociativeArrayTree('children', 'id'));

// Array
// (
//     [0] => Array
//         (
//             [id] => 1
//             [name] => node1
//             [children] => Array
//                 (
//                     [0] => Array
//                         (
//                             [id] => 2
//                             [name] => node2
//                             [children] => Array
//                                 (
//                                 )

//                         )

//                 )

//         )

// )

查看 所有类型

额外功能

物化路径级别计算示例

use Vhood\TreeType\Converter;
use Vhood\TreeType\Type\MaterializedPath;

$materializedPath = [
    [
        'name' => 'node1',
        'path' => '/1/',
    ],
    [
        'name' => 'node2',
        'path' => '/1/2/',
    ],
];

$materializedPathConverter = new Converter(new MaterializedPath($materializedPath));

print_r($materializedPathConverter->toMaterializedPath('path', '/', 'level'));

// Array
// (
//     [0] => Array
//         (
//             [name] => node1
//             [path] => /1/
//             [level] => 1
//         )

//     [1] => Array
//         (
//             [name] => node2
//             [path] => /1/2/
//             [level] => 2
//         )

// )

其他特性

  • 节点标识
  • 键重命名
  • 键删除

查看 转换接口

历史