I have been on this problem for a while now an have a manual process that I do to build the data into the array format I want, but I'm finding it time consuming and very easy to break.
So I am trying to automate the following task as best I can.
As an example, I have 2 tables in my data base, a users table and an addresses table.
The users table has a one-to-many relationship with the addresses table.
I pull down my data using a query to join the tables and pull down every row, example query:
SELECT users.name AS 'users.name',
users.age AS 'users.age',
addresses.street AS 'users.addresses.street',
addresses.suburb AS 'users.addresses.suburb'
JOIN addresses ON
users.user_id = addresses.user_id
FROM users;
If I have 2 users one with 2 addresses and one with 3 it should return 5 lines like so:
|users.name|users.age|users.addresses.street|users.addresses.suburb|
Joshua | 28 | 3 Fake St | Sydney
Joshua | 28 | 16 New St | Sydney
Joshua | 28 | 140 Apple St | Sydney
Tim | 34 | 18 John St | Sydney
Tim | 34 | 143 Clark St | Sydney
Fetching the results as an associative array would return the results in an array like the following:
array(
'users.name' => 'Joshua',
'users.age' => 28,
'users.addresses.street' => '3 Fake St',
'users.addresses.state' => 'Sydney'
),
array(
'users.name' => 'Joshua',
'users.age' => 28,
'users.addresses.street' => '16 New St',
'users.addresses.state' => 'Sydney'
),
array(
'users.name' => 'Joshua',
'users.age' => 28,
'users.addresses.street' => '140 Apple St',
'users.addresses.state' => 'Sydney'
),
array(
'users.name' => 'Tim',
'users.age' => 34,
'users.addresses.street' => '18 John St',
'users.addresses.state' => 'Sydney'
),
array(
'users.name' => 'Tim',
'users.age' => 34,
'users.addresses.street' => '143 Clark St',
'users.addresses.state' => 'Sydney'
)
What I then want to do with the above array is turn each result line into a multidimensional array like so:
array (
'users' => array(
'name' = > 'Joshua',
'age' => 28,
'addresses' =>
array(
'street' => '3 Fake St',
'state' => 'Sydney'
),
array(
'street' => '16 New St',
'state' => 'Sydney'
),
array(
'street' => '140 Apple St',
'state' => 'Sydney'
)
),
),
array (
'users' => array(
'name' = > 'Tim',
'age' => 44,
'addresses'' =>
array(
'street' => '18 John St',
'state' => 'Sydney'
),
array(
'street' => '143 Clark St',
'state' => 'Melborune'
)
),
)
As you can see each period in the array key of the database returns should be exploded, then turned into the multidimensional array, and the last exploded key should be the key value.
I have done this using array fetch and some crazy checking that gets only certain key names and puts them into an array then I'm using array_map to build the array structure... But its quite tedious for large data sets and having to write out every relationship manually and mapping it all is silly I think.
I have been attempting to build out something that fits my description above but with no luck, hence why I am now here.
NOTE
The above is an example for one set of a data, I am trying to create this so it can easily be done with multiple sets of data and not having to write a loop and manually create the multi dimensional array out.