SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow Multidimensional array inside a class?

    Hi guys can you show me how to manipulate multidimensional array both setter and getter of an array inside a class.

    Perhaps a nice sample codes that i can observe.

    Thanks in advance.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Generally speaking, Multidimensional arrays hold records of some kind, for example, the main array stores arrays indexed by their row number, and those arrays hold data indexed by their column name.

    If your multidimensional array is like this, you should consider storing it as a single array of objects (each row is a record object).

    Otherwise, can you give an example of the data you're storing in this array? Your solution can vary quite alot depending on the context.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    PHP Code:
    <?php
    class DataContainer {

        protected 
    $data;
        
        public function 
    __construct() {
            
    $this->data = array();
        }
        
        public function 
    setProperty($name,$value) {
        
            if(
    array_key_exists($name,$this->data)) {
                
    array_unshift($this->data[$name],$value);
            } else {
                
    $this->data[$name] = array($value);
            }
        
        }
        
        public function 
    getProperty($name,$key=0) {
        
            if(
    array_key_exists($name,$this->data) && array_key_exists($key,$this->data[$name])) {
                return 
    $this->data[$name][$key];
            }
        
        }

    }

    $c = new DataContainer();

    $c->setProperty('title','title1');
    $c->setProperty('title','title2');
    $c->setProperty('title','title3');

    $c->setProperty('id',9);

    echo 
    $c->getProperty('title');
    echo 
    $c->getProperty('id');

    echo 
    '<pre>',print_r($c),'</pre>';

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    Once that basic construct is in place the ArrayAccess interface and dynamic getter and setters can be added. So that the properties may be set using the alternate syntax.

    PHP Code:
    <?php
    class DataContainer implements ArrayAccess {

        protected 
    $data;
        
        public function 
    __construct() {
            
    $this->data = array();
        }
        
        public function 
    setProperty($name,$value) {
        
            if(
    array_key_exists($name,$this->data)) {
                
    array_unshift($this->data[$name],$value);
            } else {
                
    $this->data[$name] = array($value);
            }
        
        }
        
        public function 
    getProperty($name,$key=0) {
        
            if(
    array_key_exists($name,$this->data) && array_key_exists($key,$this->data[$name])) {
                return 
    $this->data[$name][$key];
            }
        
        }
        
        
    // arrayaccess interface methods
        
    public function offsetSet($offset$value) {
            
    $this->setProperty($offset,$value);
        }
        public function 
    offsetExists($offset) {
            return isset(
    $this->data[$offset]);
        }
        public function 
    offsetUnset($offset) {
            unset(
    $this->container[$offset]);
        }
        public function 
    offsetGet($offset) {
            return isset(
    $this->data[$offset]) ? $this->data[$offset][0] : null;
        }
        
        
    // dynamic getters and setters
        
    public function __get($name) {
            return 
    $this->getProperty($name);
        }
        
        public function 
    __set($name,$arg) {
            
    $this->setProperty($name,$arg);
        }

    }

    $c = new DataContainer();

    $c->setProperty('title''title1');
    $c['title'] = 'title2';
    $c['title'] ='title3';
    $c->title 'title4';

    $c['id'] = 9;
    $c->id 34;

    echo 
    $c->title;
    echo 
    $c['id'];

    echo 
    '<pre>',print_r($c),'</pre>';

  5. #5
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks guys i really appreciate your help....


Bookmarks

Posting Permissions

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