Have you ever been working with a Laravel array and thought, "I really wish there was an Arr::after() method right now"?
I did — so I submitted a PR to Laravel core.
The idea was simple: a helper that returns the element immediately after a given value in an array. The PR didn't make it in, which is completely understandable. Taylor mentioned it felt too niche for the framework core, and Laravel intentionally keeps things lean. Not every utility belongs there.
Instead of dropping the idea, I turned it into a package: Laravel Arr Extended.
The Problem
Before this, getting the next element in an array looked like this:
$items = ['a', 'b', 'c'];
$current = 'a';
$index = array_search($current, $items);
$next = isset($items[$index + 1]) ? $items[$index + 1] : null;
That's three lines of boilerplate you have to rewrite every time — and it doesn't even handle associative arrays or wrap-around. It gets messier fast.
Introducing Arr::after()
With Laravel Arr Extended, you can now do this:
use GulfarazArshad\LaravelArrExtended\Arr;
Arr::after(['a', 'b', 'c'], 'a');
// 'b'
It also supports wrap-around behavior, so the last element loops back to the first:
Arr::after(['a', 'b', 'c'], 'c', wrap: true);
// 'a'
And it works with associative arrays too:
Arr::after(['x' => 'a', 'y' => 'b'], 'a');
// 'b'
If the value isn't found in the array, it returns null — no exceptions, no surprises.
Why I Added It
I kept finding myself rewriting the same combination of array_search(), manual index calculations, and repetitive edge-case handling across projects. Arr::after() makes that logic reusable, readable, and consistent.
Installation
composer require gulfaraz-arshad/laravel-arr-extended
Check It Out
If you find it useful, a ⭐ on GitHub goes a long way. And if you've run into other array utilities you keep rewriting, I'd love to hear them — this package is growing.
Top comments (0)