【问题标题】:PHP - Searching an array or handle via database queries?PHP - 搜索数组或通过数据库查询处理?
【发布时间】:2013-01-11 07:31:27
【问题描述】:

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

【问题讨论】:

  • 是的,您应该使用数据库查询来处理搜索,而不是获取所有数据,然后让 PHP 完成工作。 PHP 处理这些事情的速度很慢,而数据库正是为此目的而构建的。

标签: php mysql arrays search


【解决方案1】:

您想要进行嵌套数组搜索,this question & it’s answers 有很多解决问题的方法。

【讨论】:

    【解决方案2】:

    看看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;
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-09
      • 2023-04-10
      • 1970-01-01
      • 2018-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      相关资源
      最近更新 更多