Converting a database result of 3 columns to associative array

equinoxe5

I have a MySQL query that returns the following result:

+----------+----------+-------+
| ApptTime | ApptType | Count |
+----------+----------+-------+
| 16:00    | pickup   | 1     |
+----------+----------+-------+
| 16:30    | dropoff  | 1     |
+----------+----------+-------+
| 16:30    | pickup   | 6     |
+----------+----------+-------+
| 16:45    | pickup   | 2     |
+----------+----------+-------+
| 16:45    | dropoff  | 1     |
+----------+----------+-------+

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:

"16:00" => "1 pickup"
"16:30" => "1 dropoff, 6 pickup" //the order in which appointments are listed is irrelevant. 
"16:45" => "1 dropoff, 2 pickup"

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.

GrumpyCrouton

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

$array = array(
    array('ApptTime' => "16:00", 'ApptType' => "pickup", 'Count' => 1),
    array('ApptTime' => "16:30", 'ApptType' => "dropoff", 'Count' => 1),
    array('ApptTime' => "16:30", 'ApptType' => "pickup", 'Count' => 6),
    array('ApptTime' => "16:45", 'ApptType' => "pickup", 'Count' => 2),
    array('ApptTime' => "16:45", 'ApptType' => "dropoff", 'Count' => 1)
);

$new_array = [];
foreach($array as $arr) {

    $time = $arr['ApptTime'];
    $type = $arr['ApptType'];
    $count = $arr['Count'];

    //check if `$time` exists as key of `$new_array`, if not, create it with an empty array value
    if(!array_key_exists($time, $new_array)) {
        $new_array[$time] = [];
    }

    //add string e.g "1 pickup" as array value to array `$new_array` with key `$time`
    $new_array[$time][] = "{$count} {$type}";
}

First loop return

Array
(
    [16:00] => Array
        (
            [0] => 1 pickup
        )

    [16:30] => Array
        (
            [0] => 1 dropoff
            [1] => 6 pickup
        )

    [16:45] => Array
        (
            [0] => 2 pickup
            [1] => 1 dropoff
        )

)

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.

foreach($new_array as &$n_arr) {
    $n_arr = implode(', ', $n_arr);
}

Second loop return

Array
(
    [16:00] => 1 pickup
    [16:30] => 1 dropoff, 6 pickup
    [16:45] => 2 pickup, 1 dropoff
)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Converting an Associative Array to a Table

From Dev

Converting array with equals to associative array

From Dev

How to use two database columns in associative array as key and value

From Dev

converting nested arrays into associative array

From Dev

Converting a result from mysqli_stmt_fetch to an associative array that could be exported by a loop by mysqli_fetch_assoc

From Dev

converting columns to postgresql database

From Dev

Converting JSON string to Associative array in PHP

From Dev

How to identify the result is an array or associative array

From Dev

array_filter converting an indexed array to an associative array

From Dev

first value in associative query result array is wrong

From Dev

Confused by result of printing associative array with PHP

From Dev

Converting rows of MYSQL database into columns

From Dev

PHP converting simple array to more complex associative array

From Dev

Store associative array in database using serialize in php

From Dev

Addressing a database as a multi-level associative array

From Dev

Addressing a database as a multi-level associative array

From Dev

Insertion of associative array into MySQL Database in php

From Dev

Converting json output from twitter search api to an associative array

From Dev

PHP how to echo certain result if associative value is not in array?

From Dev

converting Google Visualization Query result into javascript array

From Dev

Wrong Display Converting Array to XML Result in PHP

From Dev

converting Google Visualization Query result into javascript array

From Dev

Remove duplicates from associative array with multiple columns in PL/SQL

From Dev

Remove duplicates from associative array with multiple columns in PL/SQL

From Dev

How to format an associative array to columns and rows in plain text?

From Dev

Comparing database result and multidimensionnal array

From Dev

Associative Array within Associative Array

From Dev

Javascript : Sort Associative array using value property without converting into Array of objects

From Dev

Converting PHP Associative array to JSON object - spaces in value results in malformed JSON

Related Related

  1. 1

    Converting an Associative Array to a Table

  2. 2

    Converting array with equals to associative array

  3. 3

    How to use two database columns in associative array as key and value

  4. 4

    converting nested arrays into associative array

  5. 5

    Converting a result from mysqli_stmt_fetch to an associative array that could be exported by a loop by mysqli_fetch_assoc

  6. 6

    converting columns to postgresql database

  7. 7

    Converting JSON string to Associative array in PHP

  8. 8

    How to identify the result is an array or associative array

  9. 9

    array_filter converting an indexed array to an associative array

  10. 10

    first value in associative query result array is wrong

  11. 11

    Confused by result of printing associative array with PHP

  12. 12

    Converting rows of MYSQL database into columns

  13. 13

    PHP converting simple array to more complex associative array

  14. 14

    Store associative array in database using serialize in php

  15. 15

    Addressing a database as a multi-level associative array

  16. 16

    Addressing a database as a multi-level associative array

  17. 17

    Insertion of associative array into MySQL Database in php

  18. 18

    Converting json output from twitter search api to an associative array

  19. 19

    PHP how to echo certain result if associative value is not in array?

  20. 20

    converting Google Visualization Query result into javascript array

  21. 21

    Wrong Display Converting Array to XML Result in PHP

  22. 22

    converting Google Visualization Query result into javascript array

  23. 23

    Remove duplicates from associative array with multiple columns in PL/SQL

  24. 24

    Remove duplicates from associative array with multiple columns in PL/SQL

  25. 25

    How to format an associative array to columns and rows in plain text?

  26. 26

    Comparing database result and multidimensionnal array

  27. 27

    Associative Array within Associative Array

  28. 28

    Javascript : Sort Associative array using value property without converting into Array of objects

  29. 29

    Converting PHP Associative array to JSON object - spaces in value results in malformed JSON

HotTag

Archive