Skip to content
Advertisement

Converting a database result of 3 columns to associative array

I have a MySQL query that returns the following result:

JavaScript

I need to loop through the result and create a new associative array where the key is the ApptTime and the value is the combination of ApptType and Count like so:

JavaScript

I’ve tried looping through the mysqli_fetch_assoc result in a variety of ways and creating a new array within the loop but I’m fundamentally not understanding how to go about it so it’s not working out. Any help is greatly appreciated.

Advertisement

Answer

You can achieve this by using two loops. One to prepare the values for the format you want, and one to actually put the values in that format.

First, prepare the data

JavaScript

First loop return

JavaScript

And this is where I would personally stop, and format the values when actually needed to be displayed. However, the following loop can be used to modify the $new_array to the format you wanted.

JavaScript

Second loop return

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement