2010-07-15 36 views
0

请告诉我,我怎么能改善这个代码:我写了一个经常性功能的每个非阵列成员上应用用户函数给出未知深度阵列

<?php 
class TEST { 
function awalkstr($func,$a) { 
    $args = func_get_args(); array_shift($args);array_shift($args); 
    foreach ($a as &$val) { 
    if (!is_array($val)) $val = call_user_func_array($func,array_merge(array($val),$args)); 
    else $val = call_user_func_array(__METHOD__,array_merge(array($func),array($val),$args)); 
    } 
    $a = array_filter($a,function ($v) {return isset($v);}); 
    return($a); 
} 
function upper_reverse_add($str,$add) { 
    return (strlen($str)==2) ? null : $add.strtoupper(strrev($str)); 
} 
} 

$test = new TEST(); 
$testarr = array(
    'uk'=>'london', 
    array(
     'us'=>'ny', 
     array(
      'california'=>'la', 
      array(
       'tokyo', 
       array('paris'), 
       'rome' 
      ), 
      'moscow' 
     ), 
     'dublin', 
     array(
      'berlin', 
      array('warsaw') 
     ) 
    ), 
    'madrid', 
    'lapaz' 
); 
print_r($test->awalkstr(array($test,'upper_reverse_add'),$testarr,'teststr-')); 
?> 
Array (
[uk] => teststr-NODNOL 
[0] => Array 
    (
     [0] => Array 
      (
       [0] => Array 
        (
         [0] => teststr-OYKOT 
         [1] => Array 
          (
           [0] => teststr-SIRAP 
          ) 

         [2] => teststr-EMOR 
        ) 

       [1] => teststr-WOCSOM 
      ) 

     [1] => teststr-NILBUD 
     [2] => Array 
      (
       [0] => teststr-NILREB 
       [1] => Array 
        (
         [0] => teststr-WASRAW 
        ) 

      ) 

    ) 

[1] => teststr-DIRDAM 
[2] => teststr-ZAPAL) 
+1

你想改善它有什么不对? – ircmaxell 2010-07-15 18:45:48

回答

相关问题