Link to home
Start Free TrialLog in
Avatar of cmccarroll
cmccarroll

asked on

PHP multidimensional arrays

I need a recursive function to return the keys and values of a multidimensional array.
SOLUTION
Avatar of Cybervanes
Cybervanes

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cmccarroll
cmccarroll

ASKER

Your answer is close, but incomplete.  This is the array I am trying to parse.

$array = array( 'john' => array( 'position' => 'CEO', 'salary' => 240000,
'reports'=>array( 'mary' => array( 'position' => 'CIO', 'salary' => 120000,
'reports'=>array( 'james'=>array('position' => 'Sales Rep', 'salary' => 50000,
'reports'=>array() ),'jason'=>array( 'position' => 'CS Rep', 'salary' => 24000,
'reports'=>array() ),'jules'=>array( 'position' => 'CS Rep', 'salary' => 24000,
'reports'=>array() ) ) ), 'charlie'=>array( 'position' => 'CTO', 'salary' => 120000,
'reports'=>array( 'david'=>array( 'position' => 'SysAdmin', 'salary' => 75000,
'reports'=>array( 'chris' => array( 'position' => 'Tech Support', 'salary' => 24000,
'reports'=>array() ) ) ),'sharon'=>array( 'position' => 'Developer', 'salary' => 100000,
'reports'=>array() ) ) ) ) ) );
What are you trying to do with the results? Did you want to build a table?
Yes, possibly muultiple tables if appropriate.  I haven't thought that part through
What is the `reports` array?  If possible you may need to restructure the way the array is built.

array('John' => array( 'position' => 'CEO', 'salary' => 240000, 'reports' => 'Mary')
          'Mary' => array( 'position' => 'CIO', 'salary' => 120000, 'reports' => 'James')
)

Or I need a more descriptive explanation on what you are trying to accomplish.

Anybody else have any ideas?
The reports array is who the user "mary" reports to john. The user "james" reports to mary, etc. I agree the array iseems to be a little redundant, which I think is by design.  I am unable to change the existing array.  What I am tyring to accomplish is a table like this I believe;

user          position              salary         reports
john          ceo                     240000
mary         cio                      120000         john
james        Sales Rep            50000         mary
jason        CS Rep                 24000         mary
jules         CS Rep                 24000         mary
charlie      CTO                    120000         john
david        Sys Admin            75000         charlie
chirs         Tech Support       75000         david
sharron    Developer           100000         charlie
okay stand by....
I'm sorry but this one is hurting my brain!

my plan was to restructure the array to allow proper parsing...
i cant get it to work properly and I've got to move on.

there is a plethora of php experts on this site somebody should be able to come up with a solution, just give it some time.
Yea, it hurts my brain as well.  Thanks anyway.
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very Nice hielo!
Thanks a bunch.  Y'all rock!