WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
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 Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
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
Submit Site
Forex Trading Online forex trading platform

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 : New functions for PHP3. For those that know Perl, you will recognize a lot of these.All but one deal with array handling. Sorting, splicing, etc.
Categories : Algorithms, Arrays, PHP Update Picture
Shay Harding
Date : Feb 21st 1999
Grade : Be the 1st to grade this Code Example
Viewed : 5398
File : No file for this code example.
Images : No Images for this code example.
Search : More code by Shay Harding
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

OK, I finally got around to putting these all together. I was
originally creating a class to handle arrays, but functions
are easier to remember and call.



<?

#################### ADD-INS FOR PHP3 #######################
#
# These functions should look familiar to anyone that knows
# Perl. I use them all the time so I made these rather than
# have to code them over and over. There are also some
# functions that do not relate to Perl.
#
# Function list:
#
# Chomp
# Shift
# Unshift
# Pop
# Push
# Splice
# Sort unique
# Truncate array
# Get array range



################### Chomp function #####################
#
# Removes trailing line breaks from a given string
# If 'int' is specified it removes int line breaks
# which is much faster than calling chomp several
# times.
#

function chomp(&$string, $int = 1){
if (!$string || $int < 1){ return; }
for ($x = 0; $x < $int; $x++){
if (ereg("\n$",$string,$ref)){
$string = ereg_replace("\n$","",$string);
}else{
return;
}
}
unset($x);
unset($int);
}

#
########################################################


################## Shift function ######################
#
# Shifts array from left to right. This will shift off the
# zeroeth (first) element from the array and move all other
# elements to their new positions, shifted 1 space left.
#

function shift(&$array){
if (!is_array($array) || !is_string($variable)){ return; }
$variable = $array[0];
for ($x = 1; $x < count($array); $x++){
$temp[count($temp)] = $array[$x];
}
$array = $temp;
unset($temp);
return $variable;
}

#
###########################################################


################### Unshift function #####################
#
# This is the reverse of the shift function in that it will
# add $variable to the front of $array. The number of
# elements in the new array is returned.
#

function unshift(&$array,$variable){
if (!is_array($array)){ return; }
if (!is_array($variable)){
$temp = $array;
unset($array);
$array[0] = $variable;
for ($x = 0; $x < count($temp); $x++){
$array[count($array)] = $temp[$x];
}
unset($temp);
unset($x);
}elseif(is_array($variable)){
$temp = $array;
unset($array);
for ($x = 0; $x < count($variable); $x++){
$array[count($array)] = $variable[$x];
}
for ($x = 0; $x < count($temp); $x++){
$array[count($array)] = $temp[$x];
}
unset($temp);
unset($x);
}
return count($array);
}

#
#########################################################


################### Pop function #########################
#
# 'Pops' off and returns the last element of $array. By
# doing so, the size of $array is reduced by 1.
#

function pop(&$array){
if (!is_array($array)){ return; }
$variable = $array[count($array)-1];
unset($array[count($array)-1]);
return $variable;
}

#
##########################################################


#################### Push function #######################
#
# 'Pushes' $variable onto $array where $variable becomes
# the last element in $array. If $variable is an array
# then all elements of $variable are added to $array and
# $array will increase in size equal to the number of
# elements that were in $variable. The new size of $array
# is returned from this function.
#

function push(&$array,$variable){
if (!is_array($array)){ return; }
if (!is_array($variable)){
$array[count($array)] = $variable;
}elseif (is_array($variable)){
for ($x = 0; $x < count($variable); $x++){
$array[count($array)] = $variable[$x];
}
}
unset($x);
return count($array);
}

#
##########################################################


################### Splice function #####################
#
# This function lets you add elements to the beginning,
# middle, or end of an array. You can replace portions
# of an array with other arrays, or elements. If any
# variables were replaced in $array they are returned.
#

function splice(&$array,$skip,$length,$variable){
if (!is_array($array) || $length == 0){ return; }
$replaced = array();
if ($skip < 0){
if (!is_array($variable)){
$array[count($array)] = $variable;
}elseif (is_array($variable)){
for ($x = 0; $x < count($variable); $x++){
$array[count($array)] = $variable[$x];
}
}
return $replaced;
}

for ($x = 0; $x < count($array); $x++){
if ($x >= $skip){
if (!is_array($variable)){
if ($length > 1){

/* Disabled splicing of arrays since I can't figure how
to do it right now. Will have to look at this later.
The splicing of single variables still works fine.


for ($y = $skip; $y < ($length+$skip); $y++){
if ($y < count($array)){
$replaced[count($replaced)] = $array[$y];
}
}

$array[$skip] = $variable;



break;
*/

}else{
$replaced[count($replaced)] = $array[$x];
$array[$x] = $variable;
break;
}


}elseif (is_array($variable)){
for ($y = $skip; $y <= $length; $y++){
$replaced[count($replaced)] = $array[$y];
$array[$y] = $array[($skip+$length)];
}
for ($y = (count($array)-$length); $y < count($array); $y++){
unset($array[$y]);
}
$temp = $array;
unset($array);
for ($y = ($skip-1); $y >= 0; $y--){
$array[$y] = $temp[$y];
}
for ($y = $skip; $y < count($variable); $y++){
$array[count($array)] = $variable[$y];
}
for ($y = $skip; $y < count($temp); $y++){
$array[count($array)] = $temp[$y];
}
}
}
}
return $replaced;
}

#
########################################################################


################### Sort unique functions ##########################
#
# Sorts an array into unique elements. If there are arrays
# within the top level array, only the second level arrays
# are sorted. Any arrays beyond the second level are not sorted.
# if $type is specified as 'nocase' then all elements are
# sorted case-insensitive.
#

function sort_unique(&$data, $type = ""){
if (!is_array($data)){
settype($data,"array");
}
if (count($data) < 1){
print "Array is empty!<br>\n";
return;
}
if (isset($temp)){ unset($temp); }

$count = 0;
for ($x = 0; $x < count($data); $x++){
if (is_array($data[$x])){
sort_unique_sub(&$data[$x], $type);
$count++;
}
}

if ($count == 0){
sort_unique_sub(&$data, $type);
}
}

function sort_unique_sub ($data, $type = ""){
if (isset($temp)){ unset($temp); }
if ($type == "nocase"){
for ($x = 0; $x < count($data); $x++){
$data[$x] = strtolower($data[$x]);
}
}
sort($data);
for ($x = 0; $x < count($data); $x++){
if ($data[$x] != $data[$x+1]){
$temp[sizeof($temp)] = $data[$x];
}
}
unset ($data);
for ($x = 0; $x < count($temp); $x++){
$data[$x] = $temp[$x];
}
}

#
####################################################################


################ Truncate array function #########################
#
# This function will remove all elements from the specified
# element to the end of array.
#

function truncate_array(&$data, $element){
if (!is_array($data)){
settype($data,"array");
}
if (count($data) < 1){
print "Array is empty!<br>\n";
return;
}
if (!$element){
print "Must specify array element!<br>\n";
return;
}
if (count($data) < $element){
$num = count($data);
print "Array only has $num elements!<br>\n";
return;
}

for ($x = count($data); $x >= 1; $x--){
if ($x >= $element){
unset($data[$x-1]);
}
}
}

#
################################################################


############### Get array range function #######################
#
# This function will take an array and a range of elements. It
# then alters the array so that only the range of elements
# specified exist in the array.
#

function get_array_range(&$data, $range = ""){
if (!is_array($data)){
settype($data,"array");
}
if (count($data) < 1){
print "Array is empty!<br>\n";
return;
}
if (!$range){
print "No range specified!<br>\n";
return;
}

list($start,$end) = split("-",$range);
if ($start > $end){
$temp = $start;
$start = $end;
$end = $temp;
}

if ($end > count($data)){
$num = count($data);
print "Array only has $num elements!<br>\n";
return;
}

if (isset($temp)){ unset($temp); }

for ($x = 1; $x <= count($data); $x++){
if ($x >= $start && $x <= $end){
$temp[sizeof($temp)] = $data[$x-1];
}
}
unset($data);
$data = $temp;
unset($temp);

}

#
#################################################################


?>



A recursive function to traverse a multi-dimensional array where the dimensions are not known
Categories : Arrays, PHP, Algorithms
minus - subtract arrays. Send two arrays and get an array with the operation A-B, elements on A that are not included on B.
Categories : PHP, Arrays, Algorithms
A simple bubblesort that takes 2 arrays as argument.The first one is the actual data used for sorting, the second is data that will "tag along" with the first array, for instance a descriptive text about the data in the first array.
Categories : Algorithms, Arrays, PHP, Complete Programs
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
quick sort for associative arrays
Categories : Algorithms, Arrays, PHP
PHP Script to find url links in a page
Categories : PHP, URLs, Regexps, Arrays
Tag content retrieval from websites with preg_match
Categories : PHP, Regexps, Arrays, HTML and PHP
Check parameters validity. Paranoia was designed to check the validity of the parameters that a php page will receive after a form submission. It can be used to check the variables sent by POST or GET
Categories : Algorithms, HTML and PHP, PHP, Variables
Array values from javascript to php
Categories : PHP, Java Script, Arrays
clearing variables in php3
Categories : Variables, Arrays, PHP
A very simple way to build and do a hierarchical html categories browser without javascript , just using html php and mySql
Categories : HTML and PHP, Databases, Algorithms, PHP, MySQL
Weighted Random - Random Scripts usually chose one out of each item, and each item have an equal chance to be chosen. But what if you want an item to be chosed more frequently than other?
Categories : PHP, Math., Arrays
Display list of files within current and subdirectories (recursively) showing each file as an anchored link and each directory as a category header.
Categories : Filesystem, Directories, Arrays, PHP
Array Insertion
Categories : PHP, PHP Classes, Arrays
Boolean Keyword Interpreter
Categories : PHP, Algorithms, Search Engines
 Robert Bell wrote :58
Excellent!  I`m new to PHP but have been coding Perl for 
the past 2 years.  I just assumed that PHP3 had these 
functions until I got an error on a call to pop().  Used 
your code as an include file "array_functions.inc" and it 
worked the first time.

Thanks a lot!
Rob