phplyr

Guide

phplyr

phplyr is a library of functions for filtering, transforming, summarizing, joining, and reshaping tables of data in PHP. Each function takes a table and returns a new one, so a sequence of them reads as a pipeline from the raw data to the result. The functions and their arguments follow R’s dplyr and tidyr packages, and the test suite compares their results with R’s.

$schools = DataFrame::fromCsv('schools.csv');

echo $schools |>
    filter(fn ($r) => $r->study_hours >= 5) |>
    group_by('school') |>
    summarize(students: n(), avg_score: mean('score'), sd_score: sd('score')) |>
    arrange(desc('avg_score')) |>
    head(5);
$schools = DataFrame::fromCsv('schools.csv');

echo phplyr($schools)
    ->filter(fn ($r) => $r->study_hours >= 5)
    ->groupBy('school')
    ->summarize(students: n(), avg_score: mean('score'), sd_score: sd('score'))
    ->arrange(desc('avg_score'))
    ->head(5);
# A tibble: 5 × 4
  school    students avg_score sd_score
  <chr>        <int>     <dbl>    <dbl>
1 school-07       14      86.9     6.32
2 school-06       20      80.9     8.90
3 school-02       12      79.7     8.22
4 school-09       11      79.3     6.67
5 school-11       13      77.6     7.31

phplyr and phibble

The tables come from phibble, which Composer installs with phplyr. phibble reads CSV files, database queries, and PHP arrays into a typed, immutable DataFrame. Its methods, such as filter(), mutate(), groupBy(), summarize(), arrange(), and join(), are the operations every phibble table implements. They take closures, arrays of column definitions, and phibble expressions.

phplyr is built on those methods and adds:

  • dplyr’s functions and arguments, including grouped window functions such as lag() and min_rank(), across(), column selectors such as starts_with(), join_by() conditions, and the slice_*() family.
  • tidyr’s pivoting, completing, splitting, nesting, and list-column functions.
  • janitor’s clean_names(), remove_empty(), get_dupes(), and tabyl().
  • Adapters that pass a table to phit’s lm(), glm(), lmer(), and glmer().

A phplyr pipeline runs on any phibble table: the in-memory DataFrame, a database table from Phibble\tbl(), which runs the pipeline as SQL, and Phibble\Polars\PolarsTable, which runs it in Polars through the phpolars extension.

Install

composer require phpstats/phplyr

phplyr supports PHP 8.3, 8.4, and 8.5, and CI tests all three. It requires the mbstring extension. Pipelines written with the pipe operator |> need PHP 8.5. On 8.3 and 8.4, the same pipelines are method chains, as the “PHP 8.3–8.4” tabs show.

The following extensions are optional:

  • phit provides lm(), glm(), lmer(), and glmer(), and computes mean(), sd(), var_(), median(), and quantile() natively.
  • phpolars provides Polars for phibble’s PolarsTable.
  • intl transliterates names in make_clean_names() and clean_names() the way R’s janitor does.

Compatibility and limitations lists the PHP and backend details and the dplyr features phplyr does not have.

From phpstats/phplyr@d319dc5.