Skip to content
Advertisement

Change last key (dynamically named) in a multidimensional array in PHP [closed]

If my array looks like:

JavaScript

How do I change the value of [this] from something to something_else? The array can always have different keys. Other examples:

JavaScript

I want to change the last inner key. The results should be:

JavaScript

Advertisement

Answer

You can use a recursive call and access the array using a reference:

JavaScript

Output:

JavaScript

This would work with any dimension and any key name.

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