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

/*********************************************
 * Script:  Multi-Dimentional Array Handler (marray) Functions
 * 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().
 *
*********************************************/

// Process a location string
function marray_loc($loc) {
  try {
    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");
    }
  } catch(Exception $e) {
  echo "<b>Fatal Error</b>: ".$e->getMessage()." on line ".$e->getLine()." of ".$e->getFile();
  exit();
  }
}

// Set a value to the location of an array
function marray_set(&$array, $loc, $value) {
  $value=(is_array($value))?var_export($value, true):"'".addslashes($value)."'";
  eval("\$array".marray_loc($loc)."={$value};");
}

// Get the value of an array at the location
function marray_get(&$array, $loc) {
  return eval("return \$array".marray_loc($loc).";");
}

// Count the array at the location
function marray_count(&$array, $loc) {
  return eval("return count(\$array".marray_loc($loc).");");
  // Analogous to: return count(marray_get($array, $loc));
}

// Return if array is empty at location
function marray_is_empty(&$array, $loc) {
  return eval("return empty(\$array".marray_loc($loc).");");
  // Analogous to: $tmp=marray_get($array, $loc); return empty($tmp);
}

// Return if array isset at location
function marray_is_set(&$array, $loc) {
  return eval("return isset(\$array".marray_loc($loc).");");
  // Analogous to: $tmp=marray_get($array, $loc); return isset($tmp);
}
?>
Return current item: Multi-Dimensional Array Handler