Download the PHP package tomkyle/transposer without Composer
On this page you can find all versions of the php package tomkyle/transposer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tomkyle/transposer
More information about tomkyle/transposer
Files in tomkyle/transposer
Package transposer
Short Description A PHP library for transposing arrays and objects.
License MIT
Informations about the package transposer
tomkyle/transposer
A lightweight, zero-dependency utility class for transposing two-dimensional associative arrays. Converts nested data structures from [category][field] => value
format into [field][label => field, category => value]
format, making the data suitable for table display where fields become rows and categories become columns.
Installation
Install via Composer:
Requirements
- PHP 8.3 or higher
- No additional dependencies
Overview
The IterableTransposer performs matrix-like transposition on associative arrays or iterables. It is particularly useful for transforming data that is grouped by categories into a format that can be easily displayed in tables or exported to formats like CSV.
- Input: Nested array with categories containing field-value pairs
- Output: Transposed array with fields as keys, each containing category-value pairs
- Use case: Converting grouped data into table-ready format
Basic Usage
The constructor accepts an optional label for the first column of the transposal result, which can be used to describe the fields. If no label is provided, the first column will not have a label. In this example, we will use the label "Metric" for the first column in the transposed array which carries the 1st-level key names (e.g., "revenue", "orders" …).
Output:
Constructor Options
The constructor can take an optional label parameter that will be used as the header for the first column in the transposed result. If no label is provided, the first column will not have such a label.
First column will be labeled “Field Name”:
Method Parameters
__invoke($inputArr, $label = null)
- $inputArr: Array or iterable containing nested associative data
- $label: Optional runtime override for the label column name
Integration Examples
After tranposal, the data can be used in various formats such as Markdown tables, Symfony Console tables, or exported to CSV. Its result looks like so:
The result as a Markdown table would look like this:
With Symfony Console Tables
CSV Export
Error Handling
The IterableTransposer is designed to be robust and handles edge cases gracefully:
Testing
Run the test suite:
Run tests with coverage:
Run static analysis:
Development
This project follows PSR-12 coding standards and includes:
- PHPUnit for testing
- PHPStan for static analysis
- PHP-CS-Fixer for code formatting
- Rector for automated refactoring
- File watching for continuous development
Setup Development Environment
Development Workflow
The project uses npm scripts for development tasks:
File Watching
The watch command monitors source and test files for changes and automatically runs the appropriate tools:
- Source files (
src/**/*.php
): Runs PHP-CS-Fixer, PHPStan, and Rector - Test files (
tests/**/*.php
): Runs PHPUnit tests
Performance Considerations
- Memory: Creates a new array structure; original data remains unchanged
- Time Complexity: $O(n \times m)$ where $n$ = categories, $m$ = fields per category
- Best For: Small to medium datasets (thousands of records)
- Large Data: Consider streaming for very large datasets
API Reference
IterableTransposer::__construct(?string $label = null)
Creates a new transposer instance with an optional default label.
IterableTransposer::__invoke(iterable $inputArr, ?string $label = null): array
Transposes the input array and returns the result.
Parameters:
$inputArr
: Nested associative array or iterable$label
: Optional label override for the first column
Returns: Transposed associative array
Throws: No exceptions; returns empty array for invalid input
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Guidelines
- Follow PSR-12 coding standards
- Add tests for any new features
- Update documentation as needed
- Ensure all tests pass
License
This project is licensed under the MIT License - see the LICENSE file for details.
Changelog
All notable changes to this project will be documented in the CHANGELOG.md file.
Support
- GitHub Issues for bug reports and feature requests
- GitHub Discussions for questions and community support