0

I have an array and want to iterate it and apply a class method to every element. For this purpose one can use array_map(...) or array_walk(...). If it's a multidimensional array, array_walk_recursive(...) can be used.

What I have now is following case:

Array (
    [form_fieldset_aaa] => Array (
        [form_element_bbb] => Array (
            [validation_error_type_foo] => error message...
        )
        [form_fieldset_ccc] => Array (
            [form_fieldset_ddd] => Array (
                [form_fieldset_eee] => Array (
                    [form_element_fff] => Array (
                        [validation_error_type_bar] => error message...
                    )
                )
            )
            [form_fieldset_ggg] => Array (
                [1] => Array (
                    [form_fieldset_hhh] => Array (
                        [validation_error_type_buz] => error message...
                    )
                )
            )
        )
    )
)
  1. a multidimensional (associative) array;
  2. a method, that needs to be applied to every element (recursively);
  3. the method needs to get not only the current key and value, but also the additional information like the key and value of the parent element and an object for $newObject = $object->get($parentKey).

How to implement that requirement?

automatix
  • 14,018
  • 26
  • 105
  • 230
  • I'm unclear on the third requirement. Does it boil down to creating a method that accepts parameters like this: `$someObject->someMethod($key, $value, $parentKey, $parentValue, $newObject);`? – HPierce May 27 '16 at 15:31
  • @HPierce Yes, exactly. The method also need one further argument, but it's a "static" one, that means, it's something, that in a closure would be passed over `use`. `$someObject->someMethod($key, $value, $parentKey, $parentValue, $newObject, $oneFurtherArgument);` – automatix May 27 '16 at 15:35

0 Answers0