Solved

PHP multidimensional arrays

Posted on 2011-03-06
12
341 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
Comment Utility
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
Comment Utility
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
Comment Utility
What are you trying to do with the results? Did you want to build a table?
0
 

Author Comment

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

Expert Comment

by:Cybervanes
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 2

Expert Comment

by:Cybervanes
Comment Utility
okay stand by....
0
 
LVL 2

Expert Comment

by:Cybervanes
Comment Utility
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
Comment Utility
Yea, it hurts my brain as well.  Thanks anyway.
0
 
LVL 82

Accepted Solution

by:
hielo earned 400 total points
Comment Utility

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
Comment Utility
Very Nice hielo!
0
 

Author Closing Comment

by:cmccarroll
Comment Utility
Thanks a bunch.  Y'all rock!
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 …

744 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

12 Experts available now in Live!

Get 1:1 Help Now