Solved

PHP multidimensional arrays

Posted on 2011-03-06
12
342 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now