0

MySQL データベースのレコードで構成される配列を作成しました。「ネストされた」配列を含む多数のレシピが含まれています。

ユーザーが複数の用語で配列を検索し、基準を満たす配列のみを表示できるようにする最善の方法は何ですか? または、検索ごとに特定の配列を作成する必要がありますか?

配列の出力例を次に示します。

すみません、これをうまく表示する方法がわかりません....

35 => 
    array (size=17)
      'id' => string '35' (length=2)
      'name' => string 'Yummy stuff!!' (length=13)
      'recipeType' => string 'Appetizer' (length=9)
      'photo' => string 'url/recipe.gif' (length=31)
      'thumbnail_uri' => string '' (length=0)
      'published' => string '2002-04-03 00:00:00' (length=19)
      'summary' => string 'The summary of the recipe is that it is really good!' (length=52)
      'review' => string 'The review of this recipe is awesome! More please!' (length=50)
      'prepTime' => string '70' (length=2)
      'cookTime' => string '30' (length=2)
      'totalTime' => string '140' (length=3)
      'nutrition' => string 'No calories here.' (length=17)
      'instructions' => string 'I instruct you to cook it long and good, until it's warm' (length=60)
      'yield' => string 'It yields enough for me and you.' (length=32)
      'ingredient' => string '2 apples, one banana, and 18 lemons' (length=35)
      'author' => string 'John Sample Man' (length=12)
      'dietary_restrictions' => 
        array (size=2)
          6 => string 'Low fat' (length=7)
          7 => string 'Grain Free' (length=10)
36 => 
    array (size=17)
      'id' => string '36' (length=2)
      'name' => string 'A good recipe' (length=13)
      'recipeType' => string 'Appetizer' (length=9)
      'photo' => string 'url/recipe.gif' (length=31)
      'thumbnail_uri' => string '' (length=0)
      'published' => string '2002-04-03 00:00:00' (length=19)
      'summary' => string 'The summary of the recipe is that it is really good!' (length=52)
      'review' => string 'The review of this recipe is awesome! More please!' (length=50)
      'prepTime' => string '70' (length=2)
      'cookTime' => string '30' (length=2)
      'totalTime' => string '140' (length=3)
      'nutrition' => string 'No calories here.' (length=17)
      'instructions' => string 'I instruct you to cook it long and good, until it's warm' (length=60)
      'yield' => string 'It yields enough for me and you.' (length=32)
      'ingredient' => string '2 apples, one banana, and 18 lemons' (length=35)
      'author' => string 'John Sample Man' (length=12)
      'dietary_restrictions' => 
        array (size=2)
          4 => string 'Gluten-Free' (length=11)
          7 => string 'Grain Free' (length=10)
4

2 に答える 2

0

ネストされた配列検索を実行したいのですが、この質問とその回答には、問題へのアプローチが適切に選択されています。

于 2013-01-28T00:36:09.103 に答える
0

をご覧くださいarray_filter()。次のように、現在のエントリが検索用語と一致するかどうかを確認するために使用できます。

$search = Array(
    "recipeType"=>"Appetizer",
    "dietary_restrictions"=>"Low fat"
);
$results = array_filter($big_array,function($e) use ($search) {
    foreach($search as $k=>$v) {
        if( is_array($e[$k])) {
            if( !array_search($v,$e[$k])) return false;
        }
        elseif(strcasecmp($v,$e[$k]) != 0) return false;
    }
    return true;
});
于 2013-01-28T00:37:16.280 に答える