Location: PHPKode > scripts > Multi-Dimensional Array Handler > multi-dimensional-array-handler/obj.marray.php
<?php

/*********************************************
 * Script:  Multi-Dimentional Array Handler (marray) Object
 * Author:  Colin Jermain
 * Contact: http://phpknowhow.com/contact.php
 * Date:    January 25th, 2008
 * Version: 1.0
 * License: 
 *
 *    Copyright (C) 2008 Colin Jermain
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Notes:
 *  (1) Read documentation before using.
 *  (2) Exercise caution because of the use of eval().
 *
*********************************************/

class marray {

  public $array;
  
  public function marray() {
    $this->array=array();
  }
  
  // Set a value to the location of an array
  public function set($loc, $value) {
    $value=(is_array($value))?var_export($value, true):"'".addslashes($value)."'";
    eval("\$this->array".$this->loc($loc)."={$value};");    
  }
  
  // Get the value of an array at the location
  public function get($loc) {
    return eval("return \$this->array".$this->loc($loc).";");
  }
  
  // Process a location string  
  public function loc($loc) {
    try {
      if($loc!="") {
        if(preg_match('/^[A-Za-z0-9_\-]+(,[A-Za-z0-9_\-]+)*$/i', $loc)) {
        return preg_replace('/^/', '[\'', preg_replace('/$/', '\']', preg_replace('/,/', '\'][\'', $loc)));
        } else {
        throw new Exception("Invalid location path for multi-dimentional array");
        }
      } else {
      return "";
      }
    } catch(Exception $e) {
    echo "<b>Fatal Error</b>: ".$e->getMessage()." on line ".$e->getLine()." of ".$e->getFile();
    exit();
    }  
  }
  
  // Count the array at the location
  public function count($loc) {
    return eval("return count(\$this->array".$this->loc($loc).");");
    // Analogous to: return count($this->get($loc));
  }
  
  // Return if array is empty at location
  public function is_empty($loc) {
    return eval("return empty(\$this->array".$this->loc($loc).");");
    // Analogous to: $tmp=$this->get($loc); return empty($tmp);
  }

  // Return if array isset at location
  public function is_set($loc) {
    return eval("return isset(\$this->array".$this->loc($loc).");");
    // Analogous to: $tmp=marray_get($array, $loc); return isset($tmp);
  }
  
  // Print out the array
  public function __toString() {
    return print_r($this->array, true);
  }
  
}
?>
Return current item: Multi-Dimensional Array Handler