|
|
|
| Title : |
Tweak Array, insert/add elements to any position of your arrays - delete elements from your arrays - move elements within your arrays - replace elements from your arrays ... the array, 'dynamically' grows or shrinks to whatever we tweak it. |
| Categories : |
PHP Classes, Arrays, PHP |
 Holger Bahr |
| Date : |
Feb 16th 2001 |
| Grade : |
1 of 5 (graded 1 times) |
| Viewed : |
11804 |
| File : |
No file for this code example. |
| Images : |
No Images for this code example. |
|
| Search : |
More code by Holger Bahr |
|
| Action : |
Grade This Code Example
|
|
| Tools : |
My Examples List |
|
|
|
|
|
|
<?
############################################################
#######
##
## Array tweaker class (would be nice to have something like this hardcoded in
PHP :)
## 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.
##
############################################################
#######################
?>
<html>
<body>
<!--
####################### EXAMPLE #######################
please sorry for the bad coding of the example, i am in a hurry and stressed
####################### #############################
-->
<?
// include ("tweak_array.php"); (normally, we have to include/require the class)
############################################################
# 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_array, 4);
############################################################
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_array, 2, implode("\,",$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_array, 2, "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_array, 6, "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_array, 6, 2);
############################################################
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_array, 7, 13);
############################################################
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> |
|
| XML To Array Categories : PHP, PHP Classes, XML, Arrays | | | Array Insertion Categories : PHP, PHP Classes, Arrays | | | HTML_Graphs uses PHP to provide a consistent interface for creating HTML based charts. The user of the class sets up arrays that are passed to html_graph() which then takes care of all the messy HTML layout. Categories : Graphics, Arrays, PHP, PHP Classes, Charts and Graphs | | | PHPDRAW, the php wannabe Photoshop ;-) Categories : PHP, PHP Classes, GD image library, Arrays | | | Compare two texts and display a block of text with the differences between them. Categories : PHP, PHP Classes, Filesystem, Strings, Arrays | | | Sort the results from a SELECT query (any number of columns) into an array automatically. Categories : PHP, PHP Classes, Arrays, Databases, MySQL | | | A class to put get and post variables in hidden form
elements. Works on scalars, normal arrays, associative
arrays. Categories : Algorithms, Variables, Arrays, PHP, PHP Classes | | | file class , uploade file , download file already uploaded on another website Categories : PHP, PHP Classes, Filesystem, Web Services | | | Authorize.net AIM Interface Class v1.0.0 Categories : PHP, PHP Classes, Ecommerce, Payment Gateways | | | A simple class with some HTML output functions that would come in handy for consistent page layout etc. Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation | | | crop and resize image class using gd library function Categories : PHP, PHP Classes, GD image library, Graphics | | | News management class Categories : PHP, PHP Classes, Beginner Guides | | | The class to check load time of your script
VERY usefull for relatively slow applications, but not only.. Categories : PHP, PHP Classes, Debugging | | | clearing variables in php3 Categories : Variables, Arrays, PHP | | | Expose - PHP template engine, supports server and client-sided caching,a plugin system, multiple languages, template script language is based on PHP itself. Categories : PHP, PHP Classes, Templates, Complete Programs | |
| | | | Michael Browning wrote :662
This `class`, though useful, is NOT an object oriented implementation of an array. It is an example of using a `class` definition as a namespace. And, if the error variables (which are supporting properties with no real association with an array class) were removed, it could be used in the same way as any static function can be used in a class, without even the need to create an instance of the `class`...
The class itself has no intrinsic data defined. It is a package of functions that share no common information and each could stand alone outside the class scope.
No one should view this as a `good` example of using classes in PHP... it isn`t. This does NOT belong in the classes/objects section just because it uses `class`... I come here looking for interesting examples of php class utility, not repackaged functions.
I`d be curious as to why the author chose to use `class` for this implementation instead of just defining the functions independently.
I don`t mean to be harsh. I just think that there needs to be some `truth-in-posting`. There are sooo many code repositories these days that one can`t afford to waste time investigating something that claims to be one thing, and turns out to be the exact opposite.
BUT, I wish the author the BEST of luck and hope that the next version of this will truly be a useful class.
| |
|
|
|