I have table called "guids" that looks like this:
id |survey_type |guid |guid_type |table_id |created_at |
2 |TYPE 1 |6C7251E3-2151-4754-A413-51899FAAF6C2 |question |2 |2022-03-20 16:14:09 |
3 |TYPE 1 |EF5AFA93-C74D-4920-A13A-17A9B43239CD |question |3 |2022-03-20 16:14:09 |
4 |TYPE 1 |5C059148-94BE-4225-B5C2-551A81B65F16 |question |4 |2022-03-20 16:14:09 |
5 |TYPE 1 |356B8A5C-1072-47A5-A508-D9BDCBA92CCC |answer |5 |2022-03-20 16:14:09 |
6 |TYPE 1 |E0CE4C26-7ABD-4162-9C8C-B4DD540AE268 |answer |6 |2022-03-20 16:14:09 |
7 |TYPE 1 |BFBC50FC-892D-43E9-A235-D76E0D0BEF29 |answer |7 |2022-03-20 16:14:09 |
8 |TYPE 2 |B9DCC5C1-CBFB-4589-98EF-4524F3958968 |survey |8 |2022-03-20 16:14:09 |
9 |TYPE 2 |C98FBFF9-6FE3-414E-BB14-08EDC8281E66 |survey |9 |2022-03-20 16:14:09 |
10 |TYPE 2 |8A780B6E-EAE0-47D6-9D05-F52B795AE617 |question |10 |2022-03-20 16:14:09 |
11 |TYPE 2 |E3818D30-BB69-4F03-B56D-B31691F8007E |question |11 |2022-03-20 16:14:09 |
12 |TYPE 2 |24C81BEF-BFCE-4964-AB01-F3579251313D |answer |12 |2022-03-20 16:14:09 |
13 |TYPE 3 |59381701-AFBC-48F8-AECE-DB3702EE2B15 |answer |13 |2022-03-20 16:14:09 |
14 |TYPE 3 |7F4AC694-74DC-4BEA-ACFB-D8F070769FEE |answer |14 |2022-03-20 16:14:09 |
15 |TYPE 3 |B5C405B9-BA7E-471A-87DD-B69D9757276F |survey |15 |2022-03-20 16:14:09 |
From that I would like to get data in this format:
Array
(
[survey_type] => Array
(
[guid_type] => Array
(
[table_id] => guid
...
)
... // all guid types
)
... // all survey types
)
Basically, I need to use multiple columns to build a nested array with grouped data. I know that I can achive that structure using foreach loop like this:
// select existing guids
$this->db->select('*')
->from('guids');
$query = $this->db->get();
$existing_guids = $query->result_array();
$formated_guids = [];
foreach($existing_guids as $key => $guid_data) {
$formated_guids[$guid_data['survey_type']][$guid_data['guid_type']][$guid_data['table_id']] = $guid_data['guid'];
}
But if possible I would like a more optimized solution on the database layer as this table is expected to have millions of rows and looping through the entire table doesn't seem to be the most optimized way to do it.
I have played around with GROUP_CONCAT mysql command for a bit but failed to produce the desired structure
SELECT `survey_type`, GROUP_CONCAT(`guid_type`) AS `guid_types` FROM guids GROUP BY `survey_type`
Desired result with mock data:
Array
(
[TYPE 1] => Array // survey type column
(
[question] => Array // guid_type column
(
// table_id => guid
[1] => C8D21BD8-DA62-43C6-8E0D-0524D4F093B6
[2] => 6C7251E3-2151-4754-A413-51899FAAF6C2
...
)
[survey] => Array
(
[14] => B8B1F361-34A2-430E-9C0E-EB0C515B4E79
...
)
[answer] => Array
(
[2906] => 1A2F133E-0491-4117-AE2E-E3A823B66FBD...
)
),
[TYPE 2] => Array
(
... // same structure as above
)