Solved

PHP multidimensional arrays

Posted on 2011-03-06
12
346 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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 …

837 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