<?php
 
/*
 
@version .1
 
ndArray - A class that simulates a multi-dimentional database using array's.
 
Copyright (C) 2003
 
 
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 2 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, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
 
Brian Roach
 
[email protected]
 
Comments, critiques, and better ways are always welcome.
 
*/
 
class ndArray
 
{
 
 var $_aData; // Array to store data
 
 var $_aIndex; // Array to store indexes
 
 var $_aSettings; // Array to store structure
 
 var $_lastKey; // The last key to be created
 
 
 /**
 
  * @return bool
 
  * @param aDimentions array
 
  * @desc Builds structure and sets rules; Input - Array of associative arrays with keys: 'fieldName','required'
 
  */
 
 function ndArray($aDimentions)
 
  {
 
   return $this->__construct($aDimentions);
 
  }
 
 
 /**
 
  * @return bool
 
  * @param aDimentions array
 
  * @desc PHP5 constructor
 
  */
 
 function __construct($aDimentions)
 
  {
 
   if ( !is_array($aDimentions) )
 
    {
 
     return false;
 
    }
 
 
   $this->_lastKey = 0;
 
   $this->_aData = array();
 
   $this->_aIndex = array();
 
   $this->_aSettings = array();
 
   $this->_aSettings['fields'] = array();
 
 
   foreach ( $aDimentions as $key=>$data )
 
    {
 
     if ( !isset($data['fieldName']) || !isset($data['required']) )
 
      {
 
       continue;
 
      }
 
     $this->_aIndex[$data['fieldName']] = array();
 
     $this->_aSettings['fields'][$data['fieldName']] = $data['required'];
 
    }
 
   return true;
 
  }
 
 
 /**
 
  * @return array
 
  * @param iKey int
 
  * @desc Retreive a cell
 
  */
 
 function get($iKey)
 
  {
 
   $result = array();
 
   if ( is_array($iKey) )
 
    {
 
     foreach ( $iKey as $row=>$data )
 
      {
 
       if ( !isset($data['key']) || !is_array($data['key']) )
 
        {
 
         continue;
 
        }
 
 
       foreach ( $data['key'] as $keyRow=>$keyData )
 
        {
 
         $tmp = $this->get($keyData);
 
         if ( !$tmp )
 
          {
 
           continue;
 
          }
 
         $result[] = $tmp;
 
        }
 
      }
 
    }
 
 
   if ( is_int($iKey) )
 
    {
 
     $result = $this->_aData[$iKey];
 
    }
 
 
   return $result;
 
  }
 
 
 /**
 
  * @return array
 
  * @param fieldValue array||string
 
  * @param fieldName string
 
  * @desc Lookup keys matching fieldValue; (optional) fieldName - specify field to look in
 
  */
 
 function search($fieldValue, $fieldName = null)
 
  {
 
   $aKeysFound = array();
 
   $c = 0;
 
   if ( !isset($fieldName) )
 
    {
 
     foreach ( $this->_aSettings['fields'] as $field=>$bIsRequired )
 
      {
 
       if ( isset($this->_aIndex[$field][$fieldValue]) )
 
        {
 
         $aKeysFound[$c] = array();
 
         $aKeysFound[$c]['key'] = $this->_aIndex[$field][$fieldValue];
 
         $aKeysFound[$c++]['field'] = $field;
 
        }
 
      }
 
    }
 
   else
 
    {
 
     if ( isset($this->_aIndex[$fieldName][$fieldValue]) )
 
      {
 
       $aKeysFound[$c] = array();
 
       $aKeysFound[$c]['key'] = $this->_aIndex[$fieldName][$fieldValue];
 
       $aKeysFound[$c++]['field'] = $fieldName;
 
      }
 
    }
 
   return $c > 0 ? $aKeysFound : false;
 
  }
 
 
 /**
 
  * @return int
 
  * @param aData array
 
  * @param key int
 
  * @desc Store the contents of aData
 
  */
 
 function insert($aData, $key = null)
 
  {
 
   $iKey = !$key ? ++$this->_lastKey : $key;
 
 
   if ( !$this->bIsDataValid($aData) )
 
    {
 
     return false;
 
    }
 
 
   $this->_aData[$iKey] = $aData;
 
   $this->_indexAdd($iKey, $aData);
 
   return $iKey;
 
  }
 
 
 /**
 
  * @return bool
 
  * @param iKey int
 
  * @desc Delete the specified cell and any associated indexes
 
  */
 
 function delete($iKey)
 
  {
 
   if ( !isset($this->_aData[$iKey]) )
 
    {
 
     return false;
 
    }
 
   $this->_indexRemove($iKey);
 
   $this->_aData = array_delete_key($this->_aData, $iKey);
 
   return true;
 
  }
 
 
 /**
 
  * @return bool
 
  * @param iKey int
 
  * @param aCell array
 
  * @desc Create indexes for the specified key
 
  */
 
 function _indexAdd($iKey, $aCell = false)
 
  {
 
   $aData = is_array($aCell) ? $aCell : $this->get($iKey);
 
   if ( !$aData )
 
    {
 
     return false;
 
    }
 
 
   foreach ( $aData as $fieldName=>$fieldValue )
 
    {
 
     if ( !isset($this->_aIndex[$fieldName][$fieldValue]) )
 
      {
 
       $this->_aIndex[$fieldName][$fieldValue] = array();
 
      }
 
     $this->_aIndex[$fieldName][$fieldValue][] = $iKey;
 
    }
 
   return true;
 
  }
 
 
 /**
 
  * @return bool
 
  * @param iKey int
 
  * @desc Remove the indexes associated with the specified key
 
  */
 
 function _indexRemove($iKey)
 
  {
 
   if ( !$aData = $this->get($iKey) )
 
    {
 
     return false;
 
    }
 
 
   foreach ( $aData as $fieldName=>$fieldValue )
 
    {
 
     if ( !isset($this->_aIndex[$fieldName][$fieldValue]) || !is_array($this->_aIndex[$fieldName][$fieldValue]) )
 
      {
 
       continue;
 
      }
 
     $aIndex =& $this->_aIndex[$fieldName];
 
     if ( count($aIndex[$fieldValue]) > 1 )
 
      {
 
       $iTmp = array_search($iKey, $aIndex[$fieldValue]);
 
       $aIndex[$fieldValue] = array_delete_key($aIndex[$fieldValue], $iTmp);
 
      }
 
     else
 
      {
 
       $aIndex = array_delete_key($aIndex, $fieldValue);
 
      }
 
    }
 
   return false;
 
  }
 
 
 /**
 
  * @return bool
 
  * @desc Rebuild data indexes
 
  */
 
 function reindex()
 
  {
 
   if ( !isset($this->_aData) || !is_array($this->_aData) )
 
    {
 
     return false;
 
    }
 
   $oldData = $this->_aData;
 
   $this->_aData = array();
 
   $this->_aIndex = array();
 
   foreach ( $oldData as $iKey=>$aData )
 
    {
 
     if ( count($aData) > 0 )
 
      {
 
       $this->insert($aData);
 
      }
 
    }
 
 
   return true;
 
  }
 
 
 /**
 
  * @return bool
 
  * @param aData array
 
  * @desc Check to see if fields set as required are present
 
  */
 
 function bIsDataValid($aData)
 
  {
 
   $bIsValid = true;
 
   foreach ( $this->_aSettings['fields'] as $fieldName=>$bIsRequired )
 
    {
 
     if ( !$bIsRequired || !$bIsValid )
 
      {
 
       continue;
 
      }
 
     $bIsValid = strlen(trim($aData[$fieldName])) > 0 ? true : false;
 
    }
 
   return true;
 
  }
 
}
 
 
/**
 
 * @return Array
 
 * @param aInput Array
 
 * @param deleteKey mixed
 
 * @desc Delete a key from an array
 
 */
 
function array_delete_key($aInput, $deleteKey)
 
 {
 
  $aOutput = array();
 
  if ( !is_array($aInput) )
 
   {
 
    return false;
 
   }
 
 
  foreach ( $aInput as $key=>$value )
 
   {
 
    if ( $key != $deleteKey )
 
     {
 
      $aOutput[$key] = $value;
 
     }
 
   }
 
   return $aOutput;
 
 }
 
 
?>
 
 
 |