2010-07-15 3 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

답변

관련 문제