Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP multidimensional arrays

Posted on 2011-03-06
12
Medium Priority
?
356 Views
Last Modified: 2012-06-22
I need a recursive function to return the keys and values of a multidimensional array.
0
Comment
Question by:cmccarroll
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 2

Assisted Solution

by:Cybervanes
Cybervanes earned 400 total points
ID: 35046657
the following is untested

function RL($array){
	foreach($array as $key => $value){
		if(is_array($array[$key])){
			RL($array[$key]);
		}else{
			echo 'Value for '.$key.' is '.$value.'<br/>';
		}
	}
}
RL($multidimensionalarray);

Open in new window

0
 

Author Comment

by:cmccarroll
ID: 35046691
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() ) ) ) ) ) );
0
 
LVL 2

Expert Comment

by:Cybervanes
ID: 35046714
What are you trying to do with the results? Did you want to build a table?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:cmccarroll
ID: 35046732
Yes, possibly muultiple tables if appropriate.  I haven't thought that part through
0
 
LVL 2

Expert Comment

by:Cybervanes
ID: 35046795
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?
0
 

Author Comment

by:cmccarroll
ID: 35046867
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
0
 
LVL 2

Expert Comment

by:Cybervanes
ID: 35046910
okay stand by....
0
 
LVL 2

Expert Comment

by:Cybervanes
ID: 35047011
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.
0
 

Author Comment

by:cmccarroll
ID: 35047260
Yea, it hurts my brain as well.  Thanks anyway.
0
 
LVL 82

Accepted Solution

by:
hielo earned 1600 total points
ID: 35057373

function getDetails(array $array,$boss='')
{
	$str='';
	if(!$boss)
	{
		$str.='<table border="1"><thead><tr><th>user</th><th>position</th><th>salary</th><th>reports</th></tr></thead><tbody>';
	}
	foreach($array as $person=>$details)
	{
		$str.=sprintf('<tr><td>%s</td><td>%s</td><td style="text-align:right;">%s</td><td>%s</td></tr>'
				,htmlentities($person,ENT_QUOTES)
				,htmlentities($details['position'],ENT_QUOTES)
				,'$'.htmlentities(number_format($details['salary'],2),ENT_QUOTES)
				,htmlentities( $boss,ENT_QUOTES)
				);
		$str.=getDetails($details['reports'], $person);
	}
	if(!$boss)
	{
		$str.='</tbody></table>';
	}
return $str;
}
echo getDetails($array);

Open in new window

0
 
LVL 2

Expert Comment

by:Cybervanes
ID: 35057775
Very Nice hielo!
0
 

Author Closing Comment

by:cmccarroll
ID: 35059229
Thanks a bunch.  Y'all rock!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question