sarhan/php-flatten
Composer 安装命令:
composer require sarhan/php-flatten
包简介
Flattens multidimensional arrays, traversables and vars into one dimensional array.
README 文档
README
A utility function to mainly flatten multidimensional-arrays and traversables into a one-dimensional array, preserving keys and joining them with a customizable separator to from fully-qualified keys in the final array.
Installation
composer require sarhan/php-flatten
Usage
Example 1
use Sarhan\Flatten\Flatten; $multiArray = [ 'say' => 'what', 'hi' => [ 'de' => 'Hallo', 'es' => 'Hola' ] ]; /* Flatten::__construct( string $separator = '.', string $prefix = '', int $flags = 0 ) */ $flatten = new Flatten(); // Flatten::flattenToArray is provided for convinience. It internally // calls Flatten::flatten and converts it's output, which is a 1-dimensional // iterator, into a 1-dimensional array. $flattened = $flatten->flattenToArray($multiArray); // Flatten::unflattenToArray is provided for convinience. It internally // calls Flatten::unflatten and converts it's output, which is a recursive // generator structure, into a multi-dimensional array. $unflattened = $flatten->unflattenToArray($flattened); assert($flattened == [ 'say' => what 'hi.de' => Hallo 'hi.es' => Hola ]); assert($unflattened == $multiArray);
Example 2
Custom Separator and initial prefix
use Sarhan\Flatten\Flatten; $allowAccess = [ 'root' => false, 'var' => [ 'log' => ['nginx' => true, 'apt' => false], 'www' => true ], ]; $flatten = new Flatten( '/', // separator '/' // prefix ); $flattened = $flatten->flattenToArray($allowAccess); $unflattened = $flatten->unflattenToArray($flattened); assert($flatten == [ '/root' => false, '/var/log/nginx' => true, '/var/log/apt' => false, '/var/www' => true ]); assert($unflattened == $allowAccess);
Example 3
Notice that the prefix will not be separated in FQkeys. If it should be separated, separator must be appeneded to the prefix string.
use Sarhan\Flatten\Flatten; $api = [ 'category' => [ 'health' => 321, 'sport' => 769, 'fashion' => 888 ], 'tag' => [ 'soccer' => 7124, 'tennis' => [ 'singles' => 9833, 'doubles' => 27127 ] ], ]; $flatten = new Flatten('/', 'https://api.dummyhost.domain/'); $flattened = $flatten->flattenToArray($api); $unflattened = $flatten->unflattenToArray($flattened); assert($flattened == [ 'https://api.dummyhost.domain/category/health' => 321, 'https://api.dummyhost.domain/category/sport' => 769, 'https://api.dummyhost.domain/category/fashion' => 888, 'https://api.dummyhost.domain/tag/soccer' => 7124, 'https://api.dummyhost.domain/tag/tennis/singles' => 9833, 'https://api.dummyhost.domain/tag/tennis/doubles' => 27127 ]); assert($unflattened == $api);
Example 4
Numeric keys are treated as associative keys.
Note: This behavior can be changed using flags. See FLAG_NUMERIC_NOT_FLATTENED
use Sarhan\Flatten\Flatten; $nutrition = [ 'nutrition', 'fruits' => [ 'oranges', 'apple', 'banana' ], 'veggies' => ['lettuce', 'broccoli'], ]; $flatten = new Flatten('-'); $flattened = $flatten->flattenToArray($nutrition); $unflattened = $flatten->unflattenToArray($flattened); assert($flattened == [ '0' => 'nutrition', 'fruits-0' => 'oranges', 'fruits-1' => 'apple', 'fruits-2' => 'banana', 'veggies-0' => 'lettuce', 'veggies-1' => 'broccoli' ]); assert($unflattened == $nutrition);
Flags
Turns off flattening values with numeric (integer) keys.
Those values will be wrapped in an array (preserving their keys) and associated to the parent FQK.
use Sarhan\Flatten\Flatten; $examples = [ 'templates' => [ ['lang' => 'js', 'template' => "console.log('%s');"], ['lang' => 'php', 'template' => 'echo "%s";'] ], 'values' => [3 => 'hello world', 5 => 'what is your name?'] ]; $flatten = new Flatten( '.', 'examples.', Flatten::FLAG_NUMERIC_NOT_FLATTENED ); $flattened = $flatten->flattenToArray($examples); $unflattened = $flatten->unflattenToArray($flattened); assert($flattened == [ 'examples.templates' => [ [ 'lang' => 'js', 'template' => 'console.log(\'%s\')'; ], [ 'lang' => 'php', 'template' => 'echo "%s"' ] ], 'examples.values' => [ 3 => 'hello world', 5 => 'what is your name?' ] ]); assert($unflattened == $examples);
Top level numeric (integer) keys will also be returned into an array assigned to the passed prefix.
use Sarhan\Flatten\Flatten; $seats = [ 'A1', 'A2', 'B1', 'B2', '_reserved' => ['A1', 'B1'], '_blocked' => ['B2'] ]; $flatten = new Flatten( '_', 'seats', Flatten::FLAG_NUMERIC_NOT_FLATTENED ); $flattened = $flatten->flattenToArray($seats); $unflattened = $flatten->unflattenToArray($flattened); assert($flattened == [ 'seats' => ['A1', 'A2', 'B1', 'B2'], 'seats_reserved' => ['A1', 'B1'], 'seats_blocked' => ['B2'] ]); assert($unflattened == $seats);
sarhan/php-flatten 适用场景与选型建议
sarhan/php-flatten 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 184.76k 次下载、GitHub Stars 达 21, 最近一次更新时间为 2016 年 09 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「array」 「arrays」 「flatten」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sarhan/php-flatten 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sarhan/php-flatten 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sarhan/php-flatten 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Aplus Framework Image Library
Non-standard PHP library (NSPL) - functional primitives toolbox and more
A php package to flatten nested json objects and nested arrays. It also allows you to create csv files from the flattened data.
A set of useful PHP classes.
Flattens a multi-dimensional array or any \Traversable into a one-dimensional array. Elevates an one-dimensional or any \Traversable to a multi-dimensional array.
The slick/configuration component provides an easy-to-use solution for managing configuration files in your PHP applications.
统计信息
- 总下载量: 184.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 29
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2016-09-27