2017-05-17 7 views
0

PHPでオブジェクトとサブオブジェクトをループする際に助けが必要です::: 各オブジェクト配列から[permissions]要素が必要です。PHPでオブジェクトをループする

 

    object(Illuminate\Support\Collection)#21 (1) { 
     ["items":protected]=> 
     array(5) { 
     [0]=> 
     object(Discord\OAuth\Parts\Guild)#31 (5) { 
      ["id"]=> 
      string(17) "41771983423143937" 
      ["name"]=> 
      string(18) "Discord Developers" 
      ["icon"]=> 
      string(32) "edc44e98a690a1f76c5ddec68a0a6b9e" 
      ["owner"]=> 
      bool(false) 
      ["permissions"]=> 
      int(104139776) 
     } 
     [1]=> 
     object(Discord\OAuth\Parts\Guild)#22 (5) { 
      ["id"]=> 
      string(17) "81384788765712384" 
      ["name"]=> 
      string(11) "Discord API" 
      ["icon"]=> 
      string(32) "a8eccf1628b1e739d535a813f279e905" 
      ["owner"]=> 
      bool(false) 
      ["permissions"]=> 
      int(104189120) 
     } 
     [2]=> 
     object(Discord\OAuth\Parts\Guild)#37 (5) { 
      ["id"]=> 
      string(18) "159962941502783488" 
      ["name"]=> 
      string(12) "Mee6 The Bot" 
      ["icon"]=> 
      string(32) "18fbd59f2df5133bf2ec8b2d8f231a73" 
      ["owner"]=> 
      bool(false) 
      ["permissions"]=> 
      int(37080065) 
     } 
     [3]=> 
     object(Discord\OAuth\Parts\Guild)#23 (5) { 
      ["id"]=> 
      string(18) "203646825385689090" 
      ["name"]=> 
      string(6) "Jeeves" 
      ["icon"]=> 
      string(32) "77f447fb171964e1e61f706165d9f601" 
      ["owner"]=> 
      bool(false) 
      ["permissions"]=> 
      int(104193089) 
     } 
     [4]=> 
     object(Discord\OAuth\Parts\Guild)#36 (5) { 
      ["id"]=> 
      string(18) "314050405283921920" 
      ["name"]=> 
      string(7) "tesssst" 
      ["icon"]=> 
      NULL 
      ["owner"]=> 
      bool(true) 
      ["permissions"]=> 
      int(2146958591) 
     } 
     } 
    } 

基本的に、各[permissions]要素のif条件を使用してビット単位の操作チェックを実行する必要があります。

 

    if((32 & [permissions]) !== 0) { 

    // do something 

    } 

+0

あなたがアイテムにアクセスすることはできません配列はオブジェクトのインスタンスから保護されています。 'Illuminate \ Support \ Collection'オブジェクトの定義を確認してください。これはおそらくあなたが使用できるゲッターメソッドを持っているか、おそらく' ArrayAccess'のようなインターフェースを実装しています。 –

+0

@AlexHowansky、ありがとう、あなたの答えも助けた。 –

答えて

0

ここにLaravelのコレクションオブジェクトがあります。コレクションhereについての記事を読むと、例えばmap, filter, each ...

のような、それの方法のいずれかを使用し、あなたが試すことができます:

$collection->each(function ($item, $key) { 
    if ((32 & $item->permissions) !== 0) { 
     // do something 
    } 
}); 
0

あなたは、変換なしでのforEachを行うことができます。

foreach ($yourobject->item as $obj) { 
    if((32 & $obj->permissions) !== 0) { 

    // do something 

    } 
} 
関連する問題