Convert a nested array into a flat array with PHP
Posted
by Ben Fransen
on Stack Overflow
See other posts from Stack Overflow
or by Ben Fransen
Published on 2010-03-29T15:00:10Z
Indexed on
2010/03/29
15:03 UTC
Read the original article
Hit count: 330
Hello all,
I'm trying to create a generic database mapping class with PHP. Collecting the data through my functions is going well, but as expected I'm retrieving a nested set.
A print_r of my received array looks like:
Array
(
[table] => Session
[columns] => Array
(
[0] => `Session`.`ID` AS `Session_ID`
[1] => `Session`.`User` AS `Session_User`
[2] => `Session`.`SessionID` AS `Session_SessionID`
[3] => `Session`.`ExpiresAt` AS `Session_ExpiresAt`
[4] => `Session`.`CreatedAt` AS `Session_CreatedAt`
[5] => `Session`.`LastActivity` AS `Session_LastActivity`
[6] => `Session`.`ClientIP` AS `Session_ClientIP`
)
[0] => Array
(
[table] => User
[columns] => Array
(
[0] => `User`.`ID` AS `User_ID`
[1] => `User`.`UserName` AS `User_UserName`
[2] => `User`.`Password` AS `User_Password`
[3] => `User`.`FullName` AS `User_FullName`
[4] => `User`.`Address` AS `User_Address`
)
[0] => Array
(
[table] => Address
[columns] => Array
(
[0] => `Address`.`ID` AS `Address_ID`
[1] => `Address`.`UserID` AS `Address_UserID`
[2] => `Address`.`Street` AS `Address_Street`
[3] => `Address`.`City` AS `Address_City`
)
)
)
)
To simplify things I want to recreate this nested array to a flat array so I can easily loop through it and use the 'columns' key to create my SELECT query.
I'm kinda struggling with this for a while now and figures, maybe some users at SO can help me out here.
I've tried multiple things with recursion, all without luck so far...
Any help is much appriciated!
Thanks in advance, Ben Fransen
© Stack Overflow or respective owner