如何使array_walk_recursive忽略特定的键值

时间:2014-03-07 19:17:04

标签: php arrays recursion

我想制作array_walk_recursive($my_array, 'myfunction');,但前提是该密钥不包含字符串_ignore

更清楚地说明:

function Capitalize(&$input)
{
    $input = strtoupper($input);    
}

$my_array = array();    
$my_array[0]['title']="apply to this";
$my_array[0]['info']="apply to this also";
$my_array[0]['data_ignore']="don't touch this!!!!";
$my_array[1]['title']="you can change this";
$my_array[1]['info']="you can also change this";
$my_array[1]['data_ignore']="i said don't touch this! cant you see the key's name?";

array_walk_recursive($my_array, 'Capitalize');

// i wish the function to be applied to all elements except the ['data_ignore'] ones

P.S。当然,我的问题不是资本化,我只是提供一个恰当的例子。

p.s.2我试过的:我无法找到任何方法将密钥传递给函数,所以我可以在那里编程旁路:(

p.s.3所以如果你能回答“如何从引用变量中获取密钥名称?”这个问题,我的问题就会解决。

1 个答案:

答案 0 :(得分:2)

尝试

function Capitalize(&$input,$key){
    if(strpos($key,"_ignore") ===FALSE ){
      $input = strtoupper($input);    
    }
}

请参阅演示here