2012-03-30 7 views
0

私はattachメソッド内で$ this-> propertiesに値を設定するクラスを持っています。 私は簡単のため、ここでのコードを短くするだろう:私は必要なものこの配列を優先順位で並べ替える方法

<?php 
class Event 
{ 
    protected $properties = array(); 

    public function attach($event_name, $context, $event, $callback, $priority) 
    { 
     $this->properties [$event_name] = array(
       $context, 
       $event, 
       $callback, 
       $priority, 
     ); 
    } 

    public function dispatchAll($context = '0') 
    { 
     foreach($this->properties as $p) { 
      if($p[0] == $context) { 
       $this->dispatch($p[1]); 
      } else { 
       continue; 
      } 
     } 
    } 

    public function dispath($event_name) 
    { 
     // implentation not necessary for this question 
    } 
} 

は、私はascendentのために自分のアプリケーションのコンテキストに一致するコードを実行できるように、「コンテクスト、優先順位」で$this->propertiesを並べ替えることです。ちょうど3セットのプロパティのvar_dumpベロー

array(3) { 
    ["StartUp"]=> 
    array(4) { 
    [0]=> 
    string(1) "1" 
    [1]=> 
    string(7) "StartUp" 
    [2]=> 
    array(3) { 
     [0]=> 
     string(9) "Bootstrap" 
     [1]=> 
     string(7) "startup" 
     [2]=> 
     array(2) { 
     [0]=> 
     int(1) 
     [1]=> 
     object(Event)#6 (4) { 
      ["name":protected]=> 
      NULL 
      ["target":protected]=> 
      NULL 
      ["parameters":protected]=> 
      *RECURSION* 
      ["result":protected]=> 
      NULL 
     } 
     } 
    } 
    [3]=> 
    int(0) 
    } 
    ["View"]=> 
    array(4) { 
    [0]=> 
    string(1) "1" 
    [1]=> 
    string(4) "View" 
    [2]=> 
    array(2) { 
     [0]=> 
     string(9) "Bootstrap" 
     [1]=> 
     string(4) "view" 
    } 
    [3]=> 
    array(1) { 
     [0]=> 
     object(Event)#6 (4) { 
     ["name":protected]=> 
     NULL 
     ["target":protected]=> 
     NULL 
     ["parameters":protected]=> 
     *RECURSION* 
     ["result":protected]=> 
     NULL 
     } 
    } 
    } 
    ["ShutDown"]=> 
    array(4) { 
    [0]=> 
    string(1) "3" 
    [1]=> 
    string(8) "ShutDown" 
    [2]=> 
    array(2) { 
     [0]=> 
     string(9) "Bootstrap" 
     [1]=> 
     string(8) "shutdown" 
    } 
    [3]=> 
    array(1) { 
     [0]=> 
     object(Event)#6 (4) { 
     ["name":protected]=> 
     NULL 
     ["target":protected]=> 
     NULL 
     ["parameters":protected]=> 
     *RECURSION* 
     ["result":protected]=> 
     NULL 
     } 
    } 
    } 
} 
+1

'$ properties'のダンプを投稿できますか? – F21

+0

質問の横にvar_dumpを含めました –

答えて

0

usort()は、ユーザーが指定した関数で配列をソートすることができます。

また、配列がデータベースから取り込まれている場合は、クエリでORDER BYを使用して、必要な順序で要素を戻すことができます。

+0

私はすでにusort()を試しましたが、成功しませんでした...実際には多くのバリエーションがあります。だから私は上記のタイトルを投稿したのです... '彼らは多くの試練だった...私は初心者のように感じる... hehehe –

関連する問題