PHP - 通过数据库查询搜索数组或句柄?

时间:2013-01-28 00:31:18

标签: php mysql arrays search

我创建了一个由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)

2 个答案:

答案 0 :(得分:0)

您想要进行嵌套数组搜索,而this question & it’s answers可以选择很好的方法来解决问题。

答案 1 :(得分: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;
});