Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have created an array composed of records from my MySQL database. It contains a large number of recipes including a "nested" array.

What is the best way for me to allow users to search the array(s) for multiple terms and then only show the arrays that meet the criteria? Or should I try to build specific arrays for each search?

Here is a sample output of the array:

Sorry, I am not sure how to display this nicely....

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)
share|improve this question
Yes, you should handle searching with a database query, not fetch all the data, then let PHP do the work. PHP is slow with such things, while databases are built for exactly that purpose. – fab Jan 28 at 0:34

2 Answers

You want to do a nested array search and this question & it’s answers have a nice selection of approaches to the issue.

share|improve this answer

Take a look at array_filter(). You can use it to check if the current entry matches the search terms, something like this:

$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;
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.