I have an array that can vary in how many arrays deep there are, for example:
array(
'one' => array(
array(
'something' => 'value'
),
array(
'something2' => 'value2'
),
'another' => 'anothervalue'
),
'two' => array(
array(
'something' => 'value'
),
array(
'something2' => 'value2'
),
'another' => 'anothervalue'
)
)
Now, let’s say I want to replace everything with the key ‘something’.
Would I need to use a recursive function to iterate through the array? or is there a better way?
Have a look at
array_walk_recursive. It may be quite handy in a situation like this.Here’s an example using array_walk_recursive:
If used inside a class the callback method have to add the object along with the function. This is achieved with an array: