xwero / paf
pipe operator array functions
Requires
- php: ^8.3 || ^8.4 || ^8.5
- symfony/polyfill-php84: ^1.37
- symfony/polyfill-php85: ^1.37
Requires (Dev)
- pestphp/pest: ^4.6
This package is not auto-updated.
Last update: 2026-05-01 08:07:21 UTC
README
We complained for years that the array functions are messy.
- The array is the first argument in
array_filter, but the last inarray_map. - Some functions are by value and other are by reference.
And while there are good reasons those functions are how they are, they don't feel developer friendly.
Most solutions use the builder pattern. The functions in this library are meant to be used with the pipe operator.
All the function return an array.
Filter functions
filterBoth
This function replaces array_filter($arr, $filterFunction, ARRAY_FILTER_USE_BOTH).
In addition to adding the array and the filter function it is possible to reindex the keys, with or without a custom reindex function.
filterKeys
This function replaces array_filter($arr, $filterFunction, ARRAY_FILTER_USE_KEY).
filterValues
This function replaces array_filter($arr, $filterFunction).
In addition to adding the array and the filter function it is possible to reindex the keys, with or without a custom reindex function.
removeEmpty
This function replaces array_filter($arr).
In addition to adding the array it is possible to reindex the keys, with or without a custom reindex function.
Map functions
mapValues
This function replaces array_map($mapFunction, $array, ...$arrays) and array_map($mapFunction, $array).
In addition to adding the arrays and the map function it is possible to reindex the keys, with or without a custom reindex function.
mapBoth
This function replaces array_map($mapFunction, $array, array_keys($array)).
In addition to adding the array and the map function it is possible to reindex the keys, with or without a custom reindex function.
zip
This function replaces array_map(null, $array, ...$arrays).
Sort functions
sortKeys
This function replaces ksort($array, $flags) and krsort($array, $flags).
The switch between the two functions is done with the $decending boolean argument, that is by default false.
sortKeysCustom
This function replaces uksort($array, $sortFunction).
sortValues
This function replaces sort($array, $flags), rsort($array, $flags), asort($array, $flags) and
arsort($array, $flags).
The switch between the function is done by the combination of the $decending boolean argument and the $reIndex
boolean argument.
Both are false by default.
sortValuesCustom
This function replaces usort($array, $sortFunction) and uasort($array, $sortFunction).
The switch between the function is done by the $reIndex boolean argument, that is by default false.