Skip to content
Advertisement

Update multidimensional array from (Y-m) to (Y)

I would like to know if there is any way to change or update a multidimensional array from this

JavaScript

To this

JavaScript

I don’t know if this is possible but I hope someone can help me

Advertisement

Answer

You can use array_walk_recursive to walk over all the values in the array, changing those that match a YYYY-MM format to YYYY using preg_replace:

JavaScript

Note we use &$v as the argument to the callback so that we can change the values in the array.

Output:

JavaScript

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement