Meks
Do you know you can try any of our premium WordPress themes for free?Show me the themes
Recursive wp_parse_args WordPress function – merge PHP multidimensional arrays

Recursive wp_parse_args WordPress function – merge PHP multidimensional arrays

64Shares

Far all the developers, WordPress has created a nifty function called wp_parse_args which is quite useful when merging two sets of data arrays and is similar to native PHP function array_merge. The only lack of feature is that it is not recursive which makes it unable to handle and merge multidimensional arrays properly. Luckily, here is our solution.

What wp_parse_args function does?

It is very similar to the native PHP array_merge() function. From our experience so far, we found out that major usage of wp_parse_args function is when you need to pass a set of arguments as an array and make sure that default arguments which further code requires are populated. Here’s a basic example:

So far, so good. But, what if we have a two multidimensional PHP arrays we need to merge? Unfortunately, wp_parse_args function is useless in such case.

Introducing meks_wp_parse_args function (to merge multidimensional PHP arrays)

Whilst we needed to merge multidimensional arrays in some cases we have created our own function as a replacement for wp_parse_args. It will iterate through entire array and call itself recursively whenever it finds another array passed as an argument.

Top WP Hosting recommended by Meks

Usage example

Here is a real case scenario in which you will rather use our function instead of wp_parse_args.

That’s it, just copy entire meks_wp_parse_args function code inside your theme functions.php or a plugin and you can start using it. Feel free to try and give us some feedback!

64Shares
Bojan Petrovic

Co-founder and WordPress developer at Meks. He has been involved in the business since 2009. creating themes, plugins and services on top of WordPress, continuously aiming to choose best approaches and work smarter, not harder.

Try theme now for free!
  • Access to a copy of the demo website
  • Full access to the theme options
  • No credit card required

* We will send you an occasional newsletter with special offers, news & theme updates but you can unsubscribe at any time.

Meks