Php Programming Code Examples
Php > Arrays Code Examples
Array tweaker
Array tweaker
<?
## - 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
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>
<?
// 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>