2017-01-04 4 views
1

すべてのメンバーシッププランを選択するにはどのようにPHPを使用できますか?'type_id' = x and level_id = x or term_id = x?たとえば、私はすべて(type_id = 1)と基本(level_id = 1)メンバーシッププランを返すとします。私は追加のSQLクエリを避けようとしています。データは既に配列に格納されているので、私が必要とするデータを検索するのは簡単ではありません。PHPを使用して、複数の値がxに等しい配列からデータを選択するにはどうすればよいですか?

私はまた、大量のforeachループを避けようとしています。たとえば、私は最近、次のような作品を試しました...しかし、レベルを上回るタイプよりも多くのタイプがある場合はどうなりますか?より良い方法がなければなりません、そうですか?

<?php 
$available_types = array(); 
$available_levels = array(); 
foreach ($membership_plans as $plan) { 
    $available_levels[$plan->level_id] = $plan->level_name; 
    $available_types[$plan->type_id] = $plan->type_name; 
} 
$available_types = array_unique($available_types);// Unique Types 
$available_levels = array_unique($available_levels);// Unique Levels 
foreach ($available_levels as $level_id => $level_name) {// foreach level 
    foreach ($available_types as $type_id => $type_name) {// list each type with level 
     echo $level_name.' - '.$type_name; 
     // find plans tht match type and level 
     foreach ($plans as $plan) { 
      // More code goes here, but it didn't work out 
     } 
    } 
} 
?> 

私は次のような会員プランの配列を持っている:

Array 
(
    [0] => stdClass Object 
     (
      [id] => 1 
      [type_id] => 1 
      [type_name] => Single 
      [level_id] => 1 
      [level_name] => Basic 
      [term_id] => 1 
      [term_name] => MTM 
      [name] => SB_1 
      [description] => 
      [rate] => 58 
      [hidden] => 1 
      [sort] => 1000 
      [active] => 1 
     ) 

    [1] => stdClass Object 
     (
      [id] => 2 
      [type_id]`enter code here` => 1 
      [type_name] => Single 
      [level_id] => 1 
      [level_name] => Basic 
      [term_id] => 2 
      [term_name] => 12 Months 
      [name] => SB_12 
      [description] => 
      [rate] => 56 
      [hidden] => 0 
      [sort] => 1110 
      [active] => 1 
     ) 

    [2] => stdClass Object 
     (
      [id] => 14 
      [type_id] => 1 
      [type_name] => Single 
      [level_id] => 3 
      [level_name] => Premier 
      [term_id] => 2 
      [term_name] => 12 Months 
      [name] => SP_12 
      [description] => 
      [rate] => 76 
      [hidden] => 0 
      [sort] => 1310 
      [active] => 1 
     ) 
    [3] => stdClass Object 
     (
      [id] => 16 
      [type_id] => 1 
      [type_name] => Single 
      [level_id] => 3 
      [level_name] => Premier 
      [term_id] => 4 
      [term_name] => 28 Months 
      [name] => SP_28 
      [description] => 
      [rate] => 65.43 
      [hidden] => 1 
      [sort] => 1330 
      [active] => 1 
     ) 
    [4] => stdClass Object 
     (
      [id] => 19 
      [type_id] => 2 
      [type_name] => Dual 
      [level_id] => 1 
      [level_name] => Basic 
      [term_id] => 1 
      [term_name] => MTM 
      [name] => DB_1 
      [description] => 
      [rate] => 81 
      [hidden] => 1 
      [sort] => 2100 
      [active] => 1 
     ) 

) 

答えて

0

あなたはおそらく、xはあなたが使用することを持っている変数にする必要がある場合

array_filter(
    $arr, 
    function($v) { 
    return (
     $v->type_id == 'x' 
     && ($v->level_id == 'x' || $v->term_id == 'x') 
    ) 
    } 
) 

array_filter使用したいですfunction($ v)use($ var){ type_idが存在するかどうかを確認する方が良いでしょう。

関連する問題