Login   Register  
PHP Classes
elePHPant
Icontem

File: tweak_array

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Holger Bahr  >  tweak_array  >  tweak_array  >  Download  
File: tweak_array
Role: ???
Content type: text/plain
Description: this is the class
Class: tweak_array
Author: By
Last change:
Date: 2000-12-02 08:20
Size: 4,332 bytes
 

Contents

Class file image Download
<?
###################################################################
##                                                                 
## Array tweaker class                                             
## by Holger Bahr, Waldorf GmbH                    				   
##                                                                 
## ... for unique sounding electronic music gear visit us at:      
##     http://www.waldorf-music.de                                 
##                                                                 
## you can use this class if you want to                           
##                                                                 
## - delete elements of your arrays                                
## - insert elements to your arrays                                
## - move an element of your arrays to another position            
## - replace an element of your array                              
##                                                                 
## You can delete this stupid header if you use the class          
## in your software. When you add some functionality or            
## something, please drop me a line to hb@waldorf-gmbh.de          
##                                                                 
###################################################################
class tweak_array
{
	var $error_l = '<p><b>Warning:</b> Check your input parameters in your call to <i><b>';
	var $error_r = '</b></i></p>';
		
	function insert($array = '', $position = '' , $elements= '')	  	 
	{
		if ($position == '' || $array == '' || $elements == '' || $position < 1 || $position > count($array)+1)
		{
			echo $this->error_l."insert".$this->error_r;
		}
		else
		{		
			$left = array_slice ($array, 0, $position-1);
			$right = array_slice ($array, $position-1);
			$insert = explode ('\,', $elements);           		  		  
			$array = array_merge ($left, $insert, $right);
			unset($left, $right, $insert);
		}
		
		unset ($position, $elements);
		return $array;
	}
		
	function delete($array = '', $from = '', $to='')
	{
		if ($to == '') $to = $from;
		if ($array == '' || $from == '' || $to > count($array) || $to < 1 || $from > count($array))
		{						  
			echo $this->error_l."delete".$this->error_r;	
		}
		elseif ($to < $from)
		{
			echo '<p><b>Warning:</b> you provided wrong parameter relationship to <b><i>delete</i></b></p>';
		}
		else
		{				
			$left = array_slice ($array, 0, $from-1);
			$right = array_slice ($array, $to);
	
			$array = array_merge ($left, $right);
			unset ($left, $right);
		}
		
		unset ($from, $to);
		return $array;	
	}

	function move($array = '', $from = '', $to = '')
	{  										 
		if ($array == ''|| $from == ''|| $to == '' || $to > count($array) || $to < 1 || $from > count($array) || $from < 1)
		{							  
			echo $this->error_l."move".$this->error_r;
		}
		else
		{	
			$hopper = $array[$from-1];

			$array = $this->delete($array, $from);
			$array = $this->insert($array, $to, $hopper);		
		}

		unset ($hopper, $from, $to);
		return $array;
	}
	
	function replace($array = '', $position = '', $new_element = ' ')
	{  								 
		if ($array == '' || $position == '' || $position > count($array) || $position < 1)
		{					  
			echo $this->error_l."replace".$this->error_r;
		}
		else
		{
			$array = $this->insert($array, $position, $new_element);
			$array = $this->delete($array, $position+count(explode('\,', $new_element)));
		}

		unset ($position, $new_element);					
		return $array;
	}
}
###################################################################################
##                                                                                 
## Never forget the "\," when inserting multiple things into an existing  single   
## element and use implode() with the same separator "\,"  when you drop the       
## whole content of an other array, into a single element (see Example).		   
##																				   
## NEVER forget that an array starts with [0] and we say that 0 is the 1ST element.
##                                                                                 
###################################################################################
?>