开发者

How can I create an exception for a particular key in PHP's array_walk_recursive function?

开发者 https://www.devze.com 2023-02-13 16:25 出处:网络
I have a multidimensional associative array, and I want to apply array_walk_recursive so that I can execute a function on every single value.

I have a multidimensional associative array, and I want to apply array_walk_recursive so that I can execute a function on every single value.

However, whenever a key is named "special" I want to execute a different function.

So if the array is like this:

$array = array('a' => 'apple', 'b' => 'banana', 'special' => 'xylophone', 'c' => 'cherr开发者_JAVA技巧y');

Then I want to execute function doThis() on 'a', 'b', and 'c', and I want to execute doThat() on 'special'.

Is this possible?

(Note: my example is a simple array, but the real code needs to act on multidimensional array)


Your function gets passed the key as the second argument:

function foo(&$item, $key) {
    if ($key == 'special') {
        return doThat($item, $key);
    }
    return doThis($item, $key);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号