PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Holger Bahr   tweak_array   example.php   Download  
File: example.php
Role: ???
Content type: text/plain
Description: an example
Class: tweak_array
Author: By
Last change:
Date: 23 years ago
Size: 4,473 bytes
 

Contents

Class file image Download
<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_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>