dragon-code/pretty-array
Framework-agnostic PHP utility to export arrays into a readable, pretty PHP representation. Preserves numeric keys correctly (unlike symfony/var-exporter), making it ideal for generated config/translation files and other array dumps you want to commit or inspect.
Install the package via Composer:
composer require dragon-code/pretty-array
Start using it immediately by wrapping your array formatting needs. The most common first use case is formatting translation arrays (especially for Laravel HTTP status codes) where numeric keys must be preserved as strings to keep IDE autocomplete and search working. Use the Formatter service directly:
use DragonCode\PrettyArray\Services\Formatter;
$service = Formatter::make();
$array = [
'unknownError' => 'Unknown Error',
0 => 'Unknown Error',
100 => 'Continue',
101 => 'Switching Protocols',
];
echo $service->setKeyAsString()->raw($array);
This outputs properly quoted numeric keys — crucial for tools like Laravel Lang and IDEs that fail on unquoted integer keys.
Formatting for Laravel Language Files: Use setKeyAsString() + setEqualsAlign() to generate clean, IDE-safe lang/en/http-statuses.php files. The alignment makes diffs and maintenance easier.
Configuration File Generation: When generating config files from dynamic data, use setSimple() for numerically-indexed arrays or setKeyAsString() to force all keys as strings for compatibility across environments.
JSON Output for APIs/Configs: Use asJson() before formatting to output JSON instead of PHP arrays — useful for external tooling or non-PHP consumers.
CLI Scripting & Code Generation: Pipe formatted output into files using File::make($formatted)->store('path/to/file.php'). Combine with PSR-4 naming to scaffold class constants or config stubs.
Standardizing Team Output: Set default options globally via a wrapper (e.g., class AppFormatter extends Formatter) and share across projects for consistent formatting style (case conversion, alignment, etc.).
Numeric keys are always preserved as strings when setKeyAsString() is enabled — not cast to integers — which is the core purpose. Don’t expect "100" to become 100.
JSON output does not support numeric object keys — PHP objects can’t have integer property names, so they’ll be cast to strings silently (e.g., {"2":"iop"}), as shown in the README. Be cautious if expecting exact round-trip fidelity.
Case conversion applies only to string keys — numeric keys remain untouched regardless of setCase(). So '2' => 'iop' won’t change, but 'fooBar' => 1 with SNAKE_CASE becomes 'foo_bar' => 1.
setSimple() flattens associative arrays — all keys are removed and replaced with numeric indices. Perfect for logs or dump-only views, but not for translation/config files where keys matter.
Defaults are smart but not opinionated — no config files; behavior is entirely controlled at runtime. If you want global defaults, create a decorator class or static helper (e.g., app()->make(Formatter::class)->setKeyAsString()->setEqualsAlign()).
No external dependencies — it uses only dragon-code/support, meaning minimal risk of transitive vulnerabilities. Great for security-sensitive projects.
Debug tip: When debugging unexpected output, inspect the input array’s keys first with array_keys() and cast types. Mixed int/string keys are often the culprit behind formatting surprises.
How can I help you explore Laravel packages today?