rugolinifr/array-path-selector
Composer 安装命令:
composer require rugolinifr/array-path-selector
包简介
This package provides an API to flatten nested PHP arrays to map their values to a path selector, do the opposite operation or replace any value in the array.
README 文档
README
This package provides an API to flatten nested PHP arrays to map their values to a path selector, do the opposite operation or replace any value in the array.
Installation
Run the following command:
composer require rugolinifr/array-path-selector
Flattener examples
Instantiate the API object:
$flattener = (new Rugolinifr\ArrayPathSelector\Factory\PathToolFactory())->createFlattener();
Then create an array (looking like a regular JSON object in the following example):
$array = [ 'name' => 'John Doe', 'address' => [ 'city' => 'Nice', 'zips' => [ '06000', '06100', ], 'street' => 'Promenade des anglais', ], 'birth' => new DateTimeImmutable('1980-01-01 12:00:00'), 'kids' => [], ];
Then flatten an array according to the "key/value" model:
// flatten an array following the "key/value" model: $keyValues = $flattener->flattenAsKeyValues($array); $keyValues === [ 'name' => 'John Doe', 'address.city' => 'Nice', 'address.zips[0]' => '06000', 'address.zips[1]' => '06100', 'address.street' => 'Promenade des anglais', 'birth' => new DateTimeImmutable('1980-01-01 12:00:00'), 'kids' => [], //empty arrays have their own entry, otherwise they would disappear from the flattened array ];
Or following the "pairing" model:
$pairs = $flattener->flattenAsPairs($array); $keyValues === [ 0 => ['name', 'John Doe'], 1 => ['address.city', 'Nice'], 2 => ['address.zips[0]', '06000'], 3 => ['address.zips[1]', '06100'], 4 => ['address.street', 'Promenade des anglais'], 5 => ['birth', new DateTimeImmutable('1980-01-01 12:00:00')], 6 => ['kids', []], //empty arrays have their own entry, otherwise they would disappear from the flattened array ];
The "key/value" model is the more natural approach, perfect to test whether a path existence or find a value, whereas the "pairing" model suits better when comparing the subparts of two (or more) arrays is wanted, as sequential numeric keys are easier to follow than a recursive comparison algorithm.
Crud examples
Instantiate the API object:
$crud = (new Rugolinifr\ArrayPathSelector\Factory\PathToolFactory())->createCrud();
Then use it to manipulate arrays:
$array = [ 'name' => 'John Doe', 'kids' => [], ]; // Create new arrays along the property path: $crud->put($array, 'address.city', 'Nice'); $crud->put($array, 'address.zips[0]', '06000'); $array === [ 'name' => 'John Doe', 'kids' => [], 'address' => [ 'city' => 'Nice', 'zips' => [ '06000', ], ], ]; // Replace a value: $crud->put($array, 'name', 'Jane Doe'); // Delete a value: $crud->delete($array, 'kids'); $crud->delete($array, 'address.zips[0]'); $array === [ 'name' => 'Jane Doe', 'address' => [ 'city' => 'Nice', 'zips' => [], ], ];
Known limitations
Arrays having keys using the dot character of wrapping integers with squared brackets will conflicts with this API.
For maintainers
The project is shipped with a development image:
docker compose up -d --build --force-recreate
Use it to test the project:
docker compose exec php phpstan analyze docker compose exec php vendor/bin/phpunit tests
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-07-12