2014-03-25 5 views
1

나는 프로젝트의 모든 Fs_Controller_Router_Route 개체를 포함하는 $ routes 개체 배열을 가지고 있습니다.개체 요소의 배열 속성 개수를 사용하여 배열 정렬

Fs_Controller_Router_Route 모든 객체는 controller, action, params (어레이) aliasname 및 특성을 갖는다.

get 메소드 (getAction, getController, getParams, getName 및 getAlias)를 사용하여 모든 속성에 액세스 할 수 있습니다.

다음과 같은 배열에서 볼 수 있듯이, PARAMS 속성은 현재 경로의 PARAMS를 포함한 배열 .. 내가 원하는 무엇

array (size=6) 
     'home' => 
     object(Fs_Controller_Router_Route)[3] 
      private '_controller' => string 'index' (length=5) 
      private '_action' => string 'index' (length=5) 
      private '_params' => 
      array (size=2) 
       ':name' => string 'Michael' (length=7) 
       ':family_name' => string 'Corleone' (length=4) 
      private '_name' => string 'home' (length=4) 
      private '_alias' => string '/:name/:family_name' (length=19) 
     'login' => 
     object(Fs_Controller_Router_Route)[4] 
      private '_controller' => string 'index' (length=5) 
      private '_action' => string 'login' (length=5) 
      private '_params' => 
      array (size=0) 
       empty 
      private '_name' => string 'login' (length=5) 
      private '_alias' => string '/login' (length=6) 
     'signup' => 
     object(Fs_Controller_Router_Route)[5] 
      private '_controller' => string 'index' (length=5) 
      private '_action' => string 'signup' (length=6) 
      private '_params' => 
      array (size=2) 
       ':serial' => string 'dh3kddooo' (length=9) 
       ':token' => string '304888030' (length=9) 
      private '_name' => string 'signup' (length=6) 
      private '_alias' => string '/signup/:token/:serial' (length=22) 
     'about' => 
     object(Fs_Controller_Router_Route)[7] 
      private '_controller' => string 'index' (length=5) 
      private '_action' => string 'index' (length=5) 
      private '_params' => 
      array (size=2) 
       ':serial' => string 'dh3kddooo' (length=9) 
       ':token' => string '304888030' (length=9) 
      private '_name' => string 'about' (length=5) 
      private '_alias' => string '/about/:serial/:token' (length=6) 

이 사용 ascendantly 개체를 정렬하는 것입니다 모든 객체의 params의 수

이 코드를 사용하여 .. 나는 단순히 자신의 의견에 Andresch Serj로는 array_multisort 기능에

array_multisort(array_map('count', $routes), SORT_ASC, $routes); 
+1

당신이'사용 usort'와 단순한 비교 함수를 작성 간주 한 감사 작동? http://www.php.net/manual/de/function.usort.php –

+0

array_multisort를 사용해 보았는데 (왜냐하면 내가 사용했기 때문이다) usort에 대해 생각하지 않았다. 그것에 대해 생각하지 않았다. +1 – SmootQ

답변

1

감사를 카운트 PARAMS 파트를 추가하는 방법을 알고하지 않는 것이 어떻게 관리하지 않았다 .. 나는 해결책을 발견하고는

usort($routes, function($a,$b){ 

      if(count($b->getParams()) > count($a->getParams())){ 
       return 1; 
      }else{ 
       return 0; 
      } 

     }); 

+1

안녕하세요. D –