Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Pretty Array Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.


Implementation Patterns

  • 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.).


Gotchas and Tips

  • Numeric keys are always preserved as strings when setKeyAsString() is enablednot 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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport