yiisoft/arrays
yiisoft/arrays is a small PHP helper library for working with arrays. It provides safe, convenient methods to get and set values (including nested paths), filter and merge data, and simplify common array operations in Yii and any PHP project.
Strengths:
htmlEncode() improvements (via mb_encode_numericentity) remain a direct Laravel fit, particularly for UTF-8-heavy applications (e.g., Blade templates, multilingual APIs). The ~30% speedup justifies adoption in high-throughput contexts where Laravel’s native Str::htmlEntities() or e() helpers become bottlenecks. The inclusion of benchmarks in release notes strengthens the case for targeted migration.group() (e.g., @param array<int|string, mixed>) align with Laravel’s growing reliance on static analysis tools (phpstan, psalm). This improves IDE support and reduces runtime type errors, particularly in complex array manipulations.yiisoft/strings 2.6 ensures consistency with Laravel’s ecosystem, though indirect risks (e.g., encoding behavior divergence) require validation.Fit for Laravel:
htmlEncode() is a drop-in replacement for Laravel’s e() or Str::of()->htmlEntities() in high-throughput contexts (e.g., API responses, bulk data processing). Benchmark data in the release notes provides concrete justification for migration.map(), filter(), and group() complement Laravel’s Collection class without mutating state, offering additional utility for functional programming paradigms.ArrayHelper or custom Laravel helpers. Mitigate via facades or aliases (e.g., ArrayUtils::htmlEncode()).Arr, Collection, or Str facades suffice. Justify adoption for niche features (e.g., multisort(), htmlEncode() optimizations) or legacy Yii migrations.yiisoft/strings 2.6 may introduce indirect risks if Laravel’s Str facade diverges in future releases. Monitor for shared utility conflicts (e.g., htmlspecialchars behavior).ArrayHelper may require additional onboarding for advanced features (e.g., multisort()).Compatibility:
yiisoft/strings 2.6 is backward-compatible with Laravel’s existing string utilities, though indirect risks (e.g., encoding behavior) require validation.ArrayHelper behind a custom facade (e.g., ArrayUtils), isolating namespace conflicts and simplifying future updates.Key Features (Updated):
htmlEncode() now uses mb_encode_numericentity for ~30% faster UTF-8 handling, validated via release benchmarks. Critical for APIs processing multilingual data or Blade templates with user-generated content.Str::htmlEntities() in hot paths).group() now includes strict Psalm annotations, improving IDE autocompletion and static analysis integration with Laravel’s phpstan.neon.array<int, mixed>), reducing runtime type errors.self::SORT_ASC).array<int|string, mixed>).htmlEncode(), enabling comparison with Laravel’s native Str::htmlEntities():
$yiisoftTime = Benchmark::start();
$yiisoftResult = \Yii\Arrays\ArrayHelper::htmlEncode(['<script>', 'UTF-8: 你好']);
$yiisoftDuration = Benchmark::stop();
$laravelTime = Benchmark::start();
$laravelResult = Str::of(['<script>', 'UTF-8: 你好'])->map(fn($s) => e($s))->all();
$laravelDuration = Benchmark::stop();
Potential Conflicts:
yiisoft/arrays 3.2.1 and Yii 2.x’s ArrayHelper are loaded, namespace collisions persist. Resolve via:
use Yii\Arrays\ArrayHelper as YiiArrayHelper.ArrayUtils::htmlEncode()).Str::of() or Arr:: methods. Test facade integration with PHP 8.5’s new self() visibility rules.yiisoft/strings 2.6:
Str facade relies on shared string utilities (e.g., htmlspecialchars defaults). Validate via:
$yiisoftResult = \Yii\Arrays\ArrayHelper::htmlEncode(['<script>']);
$laravelResult = Str::of('<script>')->htmlEntities();
assert($yiisoftResult === $laravelResult, 'Encoding behavior mismatch');
group() or multisort() for non-associative arrays. Test with:
$nonAssocArray = [1, 2, 3];
$result = \Yii\Arrays\ArrayHelper::group($nonAssocArray, fn($item) => $item % 2);
// Ensure no type errors under PHP 8.5.
htmlEncode() reduces risk of encoding-related bottlenecks in APIs/Blade templates, with quantifiable benchmarks provided.yiisoft/strings 2.6 is a minor update, but monitor for subtle behavioral changes (e.g., encoding edge cases, shared utilities).ArrayHelper::htmlEncode() vs. Laravel’s Str::htmlEntities() in critical paths using the provided benchmarking approach.// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind('arrayUtils', function () {
return new class {
public function htmlEncode(array $array): array {
return \Yii\Arrays\ArrayHelper::htmlEncode($array);
}
public function group(array $array, callable $callback): array {
return \Yii\Arrays\ArrayHelper::group($array, $callback);
}
};
});
}
- php: 8.5
env:
LARAVEL_VERSION: 11.x-dev
htmlEncode() vs. Str::htmlEntities()).group() with non-associative arrays).How can I help you explore Laravel packages today?