Filter multidimensional array of data (2 keys) in PHP

I have a dataset in my database that looks like the following:

Food_Description DayID MealID
Poached Eggs 1 1
Meatloaf 2 2

So DayID is set up as 1=Monday, 2=Tuesday, etc. and MealID is 1=Breakfast, 2=Lunch, 3=Dinner (so the example table translates to Poached Eggs for breakfast on Monday and Meatloaf for lunch on Tuesday). Currently I have a SQL query that pulls all of the food items for the week into my PHP page using this:

$result = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_assoc($result)) {
  printf("%s \n", $row["Food_Description"]);
  echo "<br>";
}

Which produces a long list of every food item I selected for all 7 days and 3 meals. I need to be able to filter this result so that I can just output Monday’s breakfast in that cell of my HTML table and then Monday’s Lunch and Dinner followed by Tuesday’s meals, etc. I think I should be using array_filter and then using key/value pairs to get the subset of data I want but I can’t find the right syntax to write that statement. Hoping someone here can help. Thanks.

  • 1

    What you want sounds like ordering, not filtering. You want all the meals for the week, but in order by day and mealID.

    – 

  • 3

    Do that in the query: SELECT * FROM meals ORDER BY dayID, mealID

    – 

  • This is definitely a job for your query. It has everything built in to accomplish what you are looking for.

    – 

Leave a Comment