How to Swap Items in an Array Using Laravel Macros


There may be times when you are working with arrays in your PHP code and need to switch the order of two items. If you’re using an array that uses integers as keys, this can be a pretty straightforward task. However, if you’re trying to swap two items of an associative array, this can be little bit more hands-on. So, in this short post, I’ll show you how you can use macros in Laravel to keep your code looking clean and reusable.

By the end of the post, you should have a brief idea on how to create a macro that you can use to change the order of your like in this example:

What Are Macros?

In Laravel, macros are methods that can be added to classes at runtime. They can be a pretty cool way of extending the functionality of a class that you might not have full access to and might not want to inherit from. Not all classes are “macroable”, but if you want a full list of the Laravel classes that you can add macros to, check out this article written by Caleb Porzio.

Where Do I Define The Macro?

Whenever we create a new macro, we’ll need to let Laravel new that we’re providing a new macro; and we can do this inside a service provider. In theory, we can add this code to the AppServiceProvider but I like to try and keep the providers split out; otherwise, that file starts to get full pretty early on in your project. But this is just my personal opinion though, so feel free to use the AppServiceProvider if that helps you.

So, if you want to add the macro to a separate provider, let’s start by creating a new MacroServiceProvider using the following command:

We’ll then need to remember to register this service provider inside the app/config.php like below:

Now that we’ve created the new service provider, let’s create the macro. This is the macro that we’re going to be adding:

So, let’s add this macro to the service provider so that we can access it:

Now that we’ve defined the macro inside the service provider, we can swap items in arrays using the Arr::swap() method. Here are some examples below of how we would be able to use this method:

Hopefully, this short post will have given you a very brief overview of how to quickly create a macro that we can use swap two items in a PHP array. And a pretty nice benefit is that we can reuse that macro method across our system and keep our code looking readable and Laravel-y.

I’d love to hear any feedback in the comments if this helped you or not! 🚀

Originally published at

I’m a Laravel web developer based in the UK. I specialise in building websites and systems for small businesses to help them grow and increase their sales.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store