前言:最近看到一个PHP 新的 RFC。是有关在 PHP 应用中,深度嵌套数组非常常见,尤其是在处理配置数据、解码的 JSON 负载、请求数据、框架元数据或其他动态结构时的RFC。 RFC链接:
https://wiki.php.net/rfc/array_get_and_array_has。于是乎就发现了下面这个好用的数据处理框架 Flow
Flow 是一个基于 PHP 的、强类型 的数据处理框架,具有极低的内存占用。最先进的数据处理框架,支持构建可扩展的数据处理管道(pipeline),并可在各种数据源与目标之间轻松移动数据。
它专为构建大规模、可扩展的数据处理管道而设计,能帮助你在不同数据源(如数据库、CSV、JSON、数组等)和目标之间高效传输与转换数据。
composer require flow-php/etl
直接从 PHP 数组读取数据。当你的数据已经存储在内存中,比如 API 响应或测试夹具时,这非常有用。
<?php
declare(strict_types=1);
usefunctionFlow\ETL\DSL\{data_frame, from_array, to_output};
require__DIR__ . '/vendor/autoload.php';
data_frame()
->read(from_array([
['id' => 1],
['id' => 2],
['id' => 3],
['id' => 4],
['id' => 5],
]))
->collect()
->write(to_output(truncate: false))
->run();
<?php
declare(strict_types=1);
usefunctionFlow\ETL\Adapter\CSV\{from_csv, to_csv};
usefunctionFlow\ETL\DSL\{data_frame, lit, ref, sum, to_output};
useFlow\ETL\Filesystem\SaveMode;
require__DIR__ . '/vendor/autoload.php';
data_frame()
->read(from_csv(__DIR__ . '/orders_flow.csv'))
->select('created_at', 'total_price', 'discount')
->withEntry('created_at', ref('created_at')->cast('date')->dateFormat('Y/m'))
->withEntry('revenue', ref('total_price')->minus(ref('discount')))
->select('created_at', 'revenue')
->groupBy('created_at')
->aggregate(sum(ref('revenue')))
->sortBy(ref('created_at')->desc())
->withEntry('daily_revenue', ref('revenue_sum')->round(lit(2))->numberFormat(lit(2)))
->drop('revenue_sum')
->write(to_output(truncate: false))
->withEntry('created_at', ref('created_at')->toDate('Y/m'))
->mode(SaveMode::Overwrite)
->write(to_csv(__DIR__ . '/daily_revenue.csv'))
->run();
更多了解:https://flow-php.com