WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
Internet Security Software
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
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 Update Picture
Holger Bahr
Date : Feb 16th 2001
Grade : 1 of 5 (graded 3 times)
Viewed : 23667
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

Submit your own code examples  Submit your own code examples 
 

<?
############################################################
#######
##
## 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">lets 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">lets 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 arrays 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">lets 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 were 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>



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
Sort the results from a SELECT query (any number of columns) into an array automatically.
Categories : PHP, PHP Classes, Arrays, Databases, MySQL
XML To Array
Categories : PHP, PHP Classes, XML, Arrays
Compare two texts and display a block of text with the differences between them.
Categories : PHP, PHP Classes, Filesystem, Strings, Arrays
PHPDRAW, the php wannabe Photoshop ;-)
Categories : PHP, PHP Classes, GD image library, Arrays
Array Insertion
Categories : PHP, PHP Classes, Arrays
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
PHP Array to Javascript Object
Categories : PHP, Arrays, Java Script
Search and Replace Text : Searches Files for Specified Text and Replaces It by a Given Text
Categories : PHP, PHP Classes, Search, Filesystem
columned txt file to array()?
Categories : Arrays, Strings, Regexps, PHP
Pageinfo: Array containing page URI, page query string (parameters), request method (GET or POST) and the complete URI
Categories : Variables, PHP Options and Info, Arrays, URLs, PHP
Class: Info on Users, Servers and the running script
Categories : PHP, Classes and Objects, User Interface, PHP Classes
Timer - a class that uses microtime() to provide easy calculation of elapsed times
Categories : Algorithms, PHP, PHP Classes
Returns Yahoo! Address Book and Messenger List as an Array
Categories : PHP, PHP Classes, CURL
a class for doing payments to a cybercash server
Categories : Ecommerce, Complete Programs, PHP Classes, PHP
 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.