0

My SQL table is structured like this: class_id, class_name, day

I want to convert this into a multidimensional array structured as below

$days['day']['class_id']

My attempt so far is as follows:

while ($row = mysqli_fetch_assoc($result)) {
    $days[$row['day']] = $row;
}

This creates a second layer to the array based on the day, however I'm not sure how to develop that further to sort the rows based on class_id.

I then want to echo each row out based on the day using the id as the key using a foreach loop. This is what I have so far.

   foreach ($days['Monday'] as $id => $value) {

         echo "<div class='class'>";
         echo $value['class_name'];
         echo "</div>";

Any help would be massively helpful!

1 Answer 1

1
while ($row = mysqli_fetch_assoc($result)) {
    $days[$row['day']][$row['class_id']] = $row;
}

and then use it in foreach like you wrote.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.