Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post

    oop and arrayobject question

    I'm still figuring out the object oriented stuff and am looking for some advice on how to utilize ArrayObject properly on multidimensional arrays.

    The thing I need to do here is take a multidimensional array and build a function that will output the data into a table using functionality within the ArrayObject class. If it were a simple array I don't think I'd have any problem with it, but I'm trying to use getIterator and I'm just coming up short on how to pull out the actual data in this case.

    Here's my code so far...
    Code:
    <?php
    
    class userData extends ArrayObject {
    
    	public function displayAsTable() {
    		$user = array(
    				array("user_id" => "1", "first_name" => "Bob", "last_name" => "Sanders"),
    				array("user_id" => "2", "first_name" => "Dave", "last_name" => "Greggors"),
    				array("user_id" => "3", "first_name" => "Susan", "last_name" => "Bowie"),
    				array("user_id" => "4", "first_name" => "Gary", "last_name" => "Anderson")
    		);
    		$userArrayObject = new ArrayObject($user);
    		//Iterate over the values in the ArrayObject
    		echo "<table border=\"1\">";
    		for($iterator = $userArrayObject->getIterator();
    		   $iterator->valid();
    		   $iterator->next())
    		{
    		   echo "<tr><td>".$iterator->key()."</td><td>".$iterator->current()."</td></tr>";
    		}
    		echo "</table>";
    	}
    
    }
    
    userData::displayAsTable();
    ?>
    I'm guessing I need to do another for loop within the existing loop but I just can't seem to find much good info on the arrayobject stuff with examples where I can get that "aha!" moment.

    Now I know I can achieve what I want to accomplish here doing this...
    Code:
    <?php
    
    class userData extends ArrayObject {
    	public function displayAsTable() {
    		echo "<table border=\"1\"><tbody>";
    		$user = array();
    		$user[] = array("user_id" => "1", "first_name" => "Bob", "last_name" => "Sanders");
    		$user[] = array("user_id" => "2", "first_name" => "Dave", "last_name" => "Greggors");
    		$user[] = array("user_id" => "3", "first_name" => "Susan", "last_name" => "Bowie");
    		$user[] = array("user_id" => "4", "first_name" => "Gary", "last_name" => "Anderson");
    		foreach ($user as $key => $val) {
    			$userArrayObject = new ArrayObject($val);
    			foreach ($userArrayObject as $key => $val) {
    				if($key == "user_id") {
    					echo "<tr>";
    				}
    				echo "<td>".$val."</td>";
    				if($key == "last_name") {
    					echo "</tr>";
    				}
    			}
    		}
    		echo "</table></tbody>";
    	}
    }
    
    userData::displayAsTable();
    ?>
    But remember I want to use the functionality within the ArrayObject class and to give the most flexibility with the function. Basically I wan to be able to instantiate the class and set a new user with a set function and to get the data using a get function.

    I understand the basics of building a class, a function, what scope is, how to chain methods, and how to instantiate a class and modify it, etc. What I don't know yet are the ins and outs of the built in classes and functionality and how to properly use them.

    ** EDIT NOTE **
    Once I figure out how to properly iterate over the multidimensional array and set the data in objects then I'm sure I can then build out the set and get functions.
    Last edited by sfraise; 12-10-2012 at 11:32 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Are you planning on moving into more OOP? If so, worry about using a collection later on.
    Whilst you can do what you want to do, the better alternative is to deal with it as a collection of object data instead of a collection of scalar data.
    PHP Code:
    class User
    {
        private 
    $userid;
        private 
    $firstName;
        private 
    $lastName;

        public function 
    __construct($userid$firstName$lastName)
        {
            
    $this->userid $userid;
            
    $this->firstName $firstName;
            
    $this->lastName $lastName;
        }

        public function 
    getUserID()
        {
            return 
    $this->userid;
        }

        public function 
    getFirstName()
        {
            return 
    $this->firstName;
        }

        public function 
    getLastName()
        {
            return 
    $this->lastName;
        }
    }

    class 
    UserList extends ArrayObject
    {
        public function 
    displayAsTable() // or you could even override the __toString if you want.
        
    {
            
    $sOutput '<table><tbody>';
            foreach (
    $this AS $user)
            {
                
    $sOutput .= sprintf('<tr><td>%s</td><td>%s</td><td>%s</td></tr>'$user->getUserID(), $user->getFirstName(), $user->getLastName());
            }
            
    $sOutput .= print '</tbody></table>';

            return 
    $sOutput;
        }

        
    // ah, what the hell.
        
    public function __toString()
        {
            return 
    $this->displayAsTable();
        }
    }

    $ul = new UserList();
    $ul[] = new User(1'S.''Fraise');
    $ul[] = new User(2'Fou''Lu');

    print 
    $ul
    This you cannot do: userData::displayAsTable();. That is a static access whilst the method is non-static. It doesn't refer to $this, so it would actually run, but it doesn't carry a lot of use since the data it bases on is constant. You could instead write the method to be static and accept an argument: public static function displayAsTable(ArrayObject $ao);.

    One downside with this is PHP's datatype weakness. This particular collection wants user but there is no guarantee it will get it. To deal with that, you need to override the append and offsetSet methods to check for its datatype before allowing it to be added to the list. Trivial to implement.

  • Users who have thanked Fou-Lu for this post:

    sfraise (12-12-2012)

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks Fou-Lu for the help!

    I am trying to make the move over to using oop all of the time. It's clearly where everything is moving to and it obviously has some advantages. I think I have a pretty good handle on the basics, but I'm still trying to figure out how to utilize the built in classes and methods.

    I realize that my example is not really how to instantiate a class and set an object, it was just a quick and dirty way to see my output. What I really wanted to see is if it were possible to return the data through the ArrayObject class's getIterator method using a multidimensional array. To do that it seemed like the easiest thing to do was just feed it a static multidimensional array and see if I could just iterate over it, hence the quick and dirty example. I think where I'm driving myself crazy is the idea that I could use loops within a loop to do it the old fashioned way, and in my mind I should be able to create some sort of a sub-iterator within the iterator function in much that same manor, I guess it's difficult to pull myself out of that mindset.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •