Solved

PHP multidimensional arrays

Posted on 2011-03-06
12
350 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 100 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
Technology Partners: 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 400 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 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