Zend - The PHP Company




Arrays

Add Code


tweak_array  

Type: class
Added by: Holger
Entered: 21/11/2000
Last modified: 01/12/2000
Rating: - (fewer than 3 votes)
Views: 5966
esay - isert/add elements to any position of your array(s) - delete elements from your array(s) - move elements within your array(s) - replace elements from your array(s) ... the array, 'dynamically' grows or shrinks to whatever we tweak it


<?
###################################################################
##                                                                 
## 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 || $position count($array)+1)
        {
            echo 
$this->error_l."insert".$this->error_r;
        }
        else
        {        
            
$left array_slice ($array0$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 || $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 ($array0$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 || $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.
##                                                                                 
###################################################################################
?>


Usage Example


<html>
<body>
<!--     please sorry for the bad coding of the example, i am in a hurry and stressed -->

<?
include ("tweak_array.php");

############################################################ 
# we need an array to tweak and an instance of the class
############################################################ 
$our_array = array('one''2''3.646''four''5.023''six''seven''eight''nine''ten'); 
$tweak_array = new tweak_array;  
############################################################ 
?> 
<table border = "1"> 
<tr> 
<? 
echo '<td valign="top">let�s have a look at our array:<br><br>'
while (list(
$key$value) = each ($our_array))  

    echo 
"- ".$value."<br>";  

echo 
"</td>"
echo 
'<td valign="top">now we delete element 4 of that array<br><br>'

############################################################ 
# delete element 4 of the array 
############################################################ 
$our_array $tweak_array->delete($our_array4); 
############################################################ 

while (list($key$value) = each ($our_array))  

    echo 
"- ".$value."<br>";  

echo 
"</td>"
echo 
'<td valign="top">let�s add 2 new elements from on position 2<br><br>'

$newstuff = array('new_ONE''new, TWO');  
// if we want to insert new arrays (single values), we need to implode them 
// using a "," as delimitor  

#############################################################
# insert an other array�s elements into our "main" array :
############################################################ 
$our_array $tweak_array->insert($our_array2implode(",",$newstuff)); 
############################################################ 

while (list($key$value) = each ($our_array))  

    echo 
"- ".$value."<br>";  

echo 
"</td>"
echo 
'<td valign="top">let�s add another 2 new elements from on position 2<br><br>'

// now we add them manually (not inserting an array) 
// so we use still a "," as delimitor, but not on implode 

#############################################################
# insert 2 new elements :
############################################################ 
$our_array $tweak_array->insert($our_array2"new,one,new,two"); 
############################################################ 

while (list($key$value) = each ($our_array))  

    echo 
"- ".$value."<br>";  

echo 
"</td>"
echo 
'<td valign="top" nowrap>replace element 6<br><br>'

#############################################################
# replace an element with new content :
############################################################ 
$our_array $tweak_array->replace($our_array6"new_at_SIX"); 
############################################################ 

while (list($key$value) = each ($our_array))  

    echo 
"- ".$value."<br>";  

echo 
"</td>"
echo 
'<td valign="top">moving elemt 6 to position 2<br><br>';
 
#############################################################
# moving an element :
############################################################ 
$our_array $tweak_array->move($our_array62); 
############################################################ 

while (list($key$value) = each ($our_array))  

    echo 
"- ".$value."<br>";  

echo 
"</td>"
echo 
'<td valign="top">at last, we delete elements 7 to 13<br><br>'
#############################################################
# delete multiple elements (works only when they are a line of neighbours) :
############################################################ 
$our_array $tweak_array->delete($our_array713); 
############################################################ 

while (list($key$value) = each ($our_array))  

    echo 
"- ".$value."<br>";  

echo 
"</td>"
// now we unset our class because we�re finished 
unset($tweak_array); 
?> 
</tr> 
</table>
<p>
<br>
Our array here shrinks and grows to whatever we tweak it.
<p>
For example, when you got an array with 10 elements and you want
<br>
to remove one of them with unset($my_array[5]); 
<br>
that array does not shrink to 9 elements then. Element 5 is just NULL.
<br>
Or, try to insert 3 new elements from position 5 (in an 10 elemnts sized array)....
</body>
</html>


Rate This Script





Search



This Category All Categories