lukascivil/treewalker
Composer 安装命令:
composer require lukascivil/treewalker
包简介
TreeWalker is a simple and small Library that will help you to work faster with manipulation of structures in PHP
README 文档
README
A simple and lightweight PHP library for manipulating nested structures — arrays, objects and JSON strings interchangeably.
Methods
| Method | Description |
|---|---|
getdiff() |
Returns the difference between two structures |
walker() |
Walks recursively through a structure, allowing edits and deletions |
structMerge() |
Merges two structures (first argument takes precedence) |
createDynamicallyObjects() |
Creates nested keys dynamically |
getDynamicallyValue() |
Reads a value by dynamic key path |
setDynamicallyValue() |
Sets a value by dynamic key path |
Live example
All methods accept and return any of the three supported structure types:
"jsonstring" | "object" | "array"
Requirements
- PHP >= 8.1
Installation
composer require lukascivil/treewalker
Usage
Initialization
<?php $treeWalker = new TreeWalker([ "debug" => false, // true = append execution time to output "returntype" => "array" // "jsonstring" | "object" | "array" ]);
getdiff()
Returns the difference between two structures, split into new, removed and edited keys.
<?php $struct1 = ["casa" => 1, "b" => "5", "cafeina" => ["ss" => "ddd"], "oi" => 5]; $struct2 = ["casa" => 2, "cafeina" => ["ss" => "dddd"], "oi2" => 5]; $treeWalker->getdiff($struct1, $struct2, false); // false = flat slash-delimited keys
Output:
{
"new": {
"b": "5",
"oi": 5
},
"removed": {
"oi2": 5
},
"edited": {
"casa": {
"oldvalue": 2,
"newvalue": 1
},
"cafeina/ss": {
"oldvalue": "dddd",
"newvalue": "ddd"
}
}
}
Pass true as the third argument to get nested output instead of slash-delimited keys:
<?php $treeWalker->getdiff($struct1, $struct2, true); // true = nested output
walker()
Walks recursively through the structure. The callback receives the parent array, the current key and the current value by reference, allowing in-place modification or deletion.
<?php $struct = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5]; $treeWalker->walker($struct, function (&$struct, $key, &$value) { if ($key === "ff") { unset($struct[$key]); // delete node } if ($key === "ff1") { $value = ["son" => "tiago"]; // replace value } });
Output:
{"casa": 2, "cafeina": {"ss": {"ff1": {"son": "tiago"}}}, "oi2": 5}
structMerge()
Merges two structures. Values from the first argument take precedence over the second.
<?php $struct1 = ["casa" => 1, "b" => "5", "cafeina" => ["ss1" => "1", "ss2" => "2"], "oi" => 5]; $struct2 = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5, "ss" => "dddddf"]; $treeWalker->structMerge($struct1, $struct2, true); // true = nested output
Output:
{"casa": 1, "b": "5", "cafeina": {"ss1": "1", "ss2": "2", "ss": {"ff": 21, "ff1": 22}}, "oi": 5, "oi2": 5, "ss": "dddddf"}
createDynamicallyObjects()
Creates nested empty objects from a dynamic array of keys.
<?php $struct = ["casa" => 1, "b" => "5", "cafeina" => ["ss" => "ddd"], "oi" => 5]; $treeWalker->createDynamicallyObjects($struct, [1, 2, 5, 9, 10, 11]);
Output:
{
"casa": 1,
"b": "5",
"cafeina": {"ss": "ddd"},
"oi": 5,
"1": {
"2": {
"5": {
"9": {
"10": {
"11": {}
}
}
}
}
}
}
getDynamicallyValue()
Reads a value from a structure using a dynamic key path.
<?php $struct = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5]; // Static access $struct["cafeina"]["ss"]; // Dynamic access $treeWalker->getDynamicallyValue($struct, ["cafeina", "ss"]);
Output:
{"ff": 21, "ff1": 22}
setDynamicallyValue()
Sets a value in a structure using a dynamic key path.
<?php $struct = ["casa" => 2, "cafeina" => ["ss" => ["ff" => 21, "ff1" => 22]], "oi2" => 5]; // Static access $struct["cafeina"]["ss"] = "newvalue"; // Dynamic access $treeWalker->setDynamicallyValue($struct, ["cafeina", "ss"], "newvalue");
Output:
{"casa": 2, "cafeina": {"ss": "newvalue"}, "oi2": 5}
Development
composer install # install dev dependencies composer test # run tests with coverage composer check-format # PSR-2 lint composer format # PSR-2 autofix
Additional context
If you need the JavaScript equivalent for client-side structure comparison, see JsonDifference.
License
MIT © Lucas Cordeiro da Silva
lukascivil/treewalker 适用场景与选型建议
lukascivil/treewalker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.11M 次下载、GitHub Stars 达 73, 最近一次更新时间为 2016 年 06 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「array」 「diff」 「merge」 「objects」 「struct」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lukascivil/treewalker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lukascivil/treewalker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lukascivil/treewalker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library for comparing two HTML files/snippets and highlighting the differences using simple HTML.
A set of useful PHP classes.
Kinikit - PHP Application development framework MVC component
Schema Diff: Show difference between MySQL databases
ext-json wrapper with sane defaults
统计信息
- 总下载量: 1.11M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 74
- 点击次数: 39
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-06-26