ashleydawson / canonically-flatten-tree
将树(n维数组)纯函数展平为规范化的列表数组
1.0.2
2019-04-17 09:54 UTC
Requires
- php: ^7.1
Requires (Dev)
- phpunit/phpunit: ^7.5
This package is auto-updated.
Last update: 2024-08-29 05:07:27 UTC
README
纯PHP函数,将标量n维树(表示为数组)展平为单维列表(也表示为数组)。
待办事项
- 比较各种展平算法,包括 \RecursiveArrayIterator 等。
- 技术上我想我们处理的是一个嵌套集(有点类似),因此我建议在v2中更改术语以适应。
要求
此函数需要PHP >= 7.1
安装
通过 Composer 安装
$ composer req ashleydawson/canonically-flatten-tree
使用方法
该函数的基本用法如下
<?php require 'vendor/autoload.php'; $tree = [ "gamma", [ "alpha", [ "beta", ], ], [ [ [ "delta", ] ] ], ]; $list = \AshleyDawson\CanonicallyFlattenTree\canonically_flatten_scalar_tree($tree); print_r($list); /* Where output is: Array ( [0] => alpha [1] => beta [2] => delta [3] => gamma ) */
为标量添加类型检查
<?php require 'vendor/autoload.php'; // This will produce an invalid argument exception stating the nature of the type failure and at what level // In this case we're asserting that all tree nodes must be of type "string" try { \AshleyDawson\CanonicallyFlattenTree\canonically_flatten_scalar_tree(['alpha', [8]], 'string'); } catch (\InvalidArgumentException $e) { var_dump($e); }
测试 & 基准测试
使用 PHPUnit 进行测试
$ vendor/bin/phpunit -c .
使用 Blackfire 进行基准测试
$ blackfire run php vendor/bin/phpunit
可以找到基准测试报告示例。