I have this array structure. The ID 145 is a request with two associated relations to another table - the project id 16 and 17. So, one request can have multiples projects.
Now I want to change this structure and merge the duplicated info. I mean, merge [0] and [n]. So funding_project_name and relation_id will become two entries for a new sub-array.
[145] => Array
(
[0] => Array
(
[firstname] => John
[institution] => Harvard
[funding_project_name] => Project 1 #add to array funding_project_name - [0]
[relation_id] => 16 #add to array relation_id - [0]
)
[1] => Array
(
[firstname] => John //discard
[institution] => Harvard //discard
[funding_project_name] => Project 2 #add to array funding_project_name - [1]
[relation_id] => 17 #add to array relation_id - [1]
)
)
I achieved this output using:
$query->fetchAll(PDO::FETCH_GROUP|PDO::FETCH_ASSOC)
Any idea how can I get the intended structure? Already tried some nested foreach, however not sure how to group the items
Expected:
[145] => Array
(
[firstname] => John
[institution] => Harvard
[projects] => Array(
[0] => Array(
[funding_project_name] => Project 1
[relation_id] => 16
)
[1] => Array(
[funding_project_name] => Project 2
[relation_id] => 17
)
)
)