Multidimensional Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Multidimensional Array

Post by mrvanjohnson »

OK, I'll admit arrays are by no way my strong point, and I usually try to avoid them, but it's time I address my fears and hit it head on.

An application I am working on has me configuring a fairly complex Array (which I might have done wrong in itself)

Here is the array of employees

Code: Select all

<?php
$plantA = array(
	"Mike Mann" => array(
						"Roles" =>array ("Owner" => "Owner", "CEO" => "CEO"),
						"Phone" => "(555) 555-5555",
						"Email" => "mike@plantA.com"),
	"Sam Newal"  => array(
						"Roles" =>array ("Owner" => "Owner", "CTO" => "CTO"),
						"Phone" => "(555) 555-5554",
						"Email" => "sam@plantA.com"),							
	"Bob Allxon" => array(
						"Roles" => array ("Safety Manager" => "Safety Manager", 
													"Line Manager" => array ("Line1", "Line2", "Line3")),
						"Phone" => "(555) 555-5556",
						"Email" => "bob@plantA.com"),
	"Steve Dunn" => array(
 						"Roles" => array ("Line Manager" => array ("Line4", "Line5", "Line6")),
						"Phone" => "",
						"Email" => "steve@plantA.com")
				);
?>
Now I can do a print_r ($plantA); and see that the array has been constructed. I have even figured out how to get at some of the array using the foreach statement

Code: Select all

<?php
foreach ($plantA as $key => $plantA)
					{
						print				("$key - ");
						foreach ($plantA as $key2 => $info)
						{
							print				("$info <br> \n");
	
						}
					}
?>
But I'm afraid I am stuck at that point, I am having problems how to visualize what I need to get done. Basically there are two things I'm looking to do

1) Print out the directory of employees in basic columns of Name Role Phone Email. I got a lot of this licked but I can't figure out how to logically pull all the information from each array.. Mainly how to permeate through the Roles array on each person.

2) Also, on each individual "Line's" page, display that Line's Manager. For example on the page for Line3 it would display the manager as Bob Allxon. I'm not sure how to perform that type of search function on an array to get that result.

Any pointers would be greatly appreciated even it was to redesign the array. Database is not an option for this site besides, like I said I would really appreciate understanding arrays.
User avatar
Sevengraff
Forum Contributor
Posts: 232
Joined: Thu Apr 25, 2002 9:34 pm
Location: California USA
Contact:

Post by Sevengraff »

You may want to just make a employee class and try working with an array of those, but I think you can still do it this way.

Code: Select all

$plantA = array(
   "Mike Mann" => array(
                  "Roles" =>array ("Owner" => "Owner", "CEO" => "CEO"),
                  "Phone" => "(555) 555-5555",
                  "Email" => "mike@plantA.com"),
   "Sam Newal"  => array(
                  "Roles" =>array ("Owner" => "Owner", "CTO" => "CTO"),
                  "Phone" => "(555) 555-5554",
                  "Email" => "sam@plantA.com"),                     
   "Bob Allxon" => array(
                  "Roles" => array ("Safety Manager" => "Safety Manager",
                                       "Line Manager" => array ("Line1", "Line2", "Line3")),
                  "Phone" => "(555) 555-5556",
                  "Email" => "bob@plantA.com"),
   "Steve Dunn" => array(
                  "Roles" => array ("Line Manager" => array ("Line4", "Line5", "Line6")),
                  "Phone" => "",
                  "Email" => "steve@plantA.com")
            ); 

foreach( $plantA as $name => $data ) {
    echo "<p>Employee $name:<br>";
    foreach($data as $item => $data ) {
        echo "<b>$item</b> ";
        if( $item == 'Roles') {
             foreach($data as $role ) 
                   echo $role;
        } else {
           echo $data;
        }
        echo '<br>';
     }
}
You could either add more checks for the other roles, or do something recursive with if( is_array($data) ) {blah...}

I think you should look into doing this oop
User avatar
mrvanjohnson
Forum Contributor
Posts: 137
Joined: Wed May 28, 2003 11:38 am
Location: San Diego, CA

Post by mrvanjohnson »

Thanks Sevengraff

I will try your code tonight. I am most definiently going OOP with this when I figure out the code. I don't want to have to code this more than I need to :-)

Thanks again
Post Reply