Source for file Arrays.class.php
Documentation is available at Arrays.class.php
* For including this file you have to define the constant "CLASSPATH".
* Because every include in the framework depends on the CLASSPATH definition.
* The CLASSPATH means the relative path to the folder that contains the
echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
* Including language specific values.
include_once( CLASSPATH. "core/lang_spec_values/". LANG. ".inc.php" );
echo "<h3>You have to define the constant LANG!</h3>\r\n";
echo "Example for german: define( 'LANG', 'de' );\r\n";
include_once( CLASSPATH. "core/Numbers.class.php" );
* Static methods to manipulate arrays.
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* Removes the record at $int-index.
* @param integer $int Index of record to remove.
* @param array $array The array in which the record should be removed.
* @return array The removed index in an one dimensional array
* Searches the first record of $value. If it is found it will be removed
* else the unchanged array will be given back.
* @param mixed $value The value that should be searched.
* @param array $array The array in which the record should be removed.
* @return array The removed index in an one dimensional array
for ( $i = 0; $i < count( $array ); $i++ )
if ( $array[$i] == $value )
* Returns whether an array is empty. That means every record of the array
* will be proofed with the function empty().
* @param array $array The array that should be checked of emptiness.
{ return empty( $array ); }
foreach ( $array as $key => $val )
* Returns the given array as an index based array.
* @param array $array The array that should be converted.
while ( list ( $key, $val ) = each( $array ) )
* Returns a new array with the values from the given key of the second
* dimension as keys in first dimension refering to the second dimension
* 0 => array("id"=>24,"name"=>"Vera","name"=>"Oberkönig"),
* 1 => array("id"=>17,"name"=>"Daniel","name"=>"Plücken"),
* 2 => array("id"=>23,"name"=>"Kayleigh","name"=>"Moritz")
* $p_data_arr = Arrays::primaryValueToTuple( $data_arr, "id" );
* "24" => array("id"=>24,"name"=>"Vera","name"=>"Oberkönig"),
* "17" => array("id"=>17,"name"=>"Daniel","name"=>"Plücken"),
* "23" => array("id"=>23,"name"=>"Kayleigh","name"=>"Moritz")
* @param string $value_key
foreach ( $array as $key => $val_arr )
{ $out_arr[ $val_arr[$value_key] ] = $array[$key]; }
* Converts a multidimensional array in a one dimensional array.
* @author bluej100 at gmail dot com
* @see http://www.php.net/manual/en/function.array-values.php#77671
* @author Daniel Plücken <daniel@debakel.net> (some modifications)
* @param integer $preserve_keys
function &flatten( &$array, &$newArray, $preserve_keys = 1 )
foreach ( $array as $key => $child )
$child[$key], $newArray, $preserve_keys
if ( $preserve_keys + is_string( $key ) > 1 )
{ $newArray[$key] = $child; }
{ $newArray[] = $child; }
* Returns the position of the first record that matches the value
* of $mixedVal. If there is not a record that matches the value
* then the function will return false.
* @param mixed $mixedVal The value that should be searched.
* @param array $array The array in which the record should be
for ( $i = 0; $i < count( $array ); $i++ )
if ( $mixedVal->equals( $array[$i] ) )
for ( $i = 0; $i < count( $array ); $i++ )
if ( $array[$i] == $mixedVal )
* Returns a random record of given $array. Sets automatically a new random
* set by microtime for real random.
* @param array $array The array from which the record should be returned.
* Returns true if $array contains $record else it will return false.
* @param array $array The array in which schould be searched.
* @param mixed $record The value which should compare with the records
* Fills $record in $array only if $array does not already contains it.
* @param array $array The array in which the record should be filled in.
* @param mixed $record The value which should be filled in the array.
for ( $i = 0; $i < count( $array ); $i++ )
if ( $array[$i] == $record )
* Removes doubly occuring records in $array and set up new indexes, so that
* the first index of the array beginns at zero and the other indexes
* following integrated and ascendending.
* @param array $array The array in which each record should be unique.
foreach( $array as $tmp )
* Returns the given second dimension index of a two dimensional array as
* a one dimensional array. Imagine that a two dimensional array is pictured
* as a table and you want to get all values from a specified col of this
* table, then you can use this function to do so. It is very helpful to
* extract all values of a field from a set of data.
* @param integer/string $index
* @param array $array The array of which the index
* @param boolean $empty_entries Decides wether empty entries
* should add to the new array or
for ( $i = 0; $i < count( $array ); $i++ )
|| !empty( $array[$i][$index] )
{ $arr[] = $array[$i][$index]; }
* Returns the values behind all keys that correspond with the given one.
* @param string $str_key_name
foreach ( $in_arr as $key => $value )
if ( $key === $str_key_name )
* Returns the first occuring value that is a type of integer.
foreach ( $in_arr as $key => $value )
* Returns the first occuring value that matches a regular expression.
* @param string $str_reg_exp
foreach ( $in_arr as $key => $value )
* Merges two arrays to one. All records that occuring in both arrays will
if ( count( $array1 ) < 1 )
if ( count( $array2 ) < 1 )
for ( $i = 0; $i < count( $array1 ); $i++ )
for ( $j = 0; $j < count( $array2 ); $j++ )
if ( $array1[$i] == $array2[$j] )
{ $newArray[] = $array1[$i]; }
for( $i = 0; $i < count( $array2 ); $i++ )
for ( $j = 0; $j < count( $array1 ); $j++ )
if ( $array2[$i] == $array1[$j] )
{ $newArray[] = $array2[$i]; }
* Returns whether the records in the array are consecutively numbered
* in a given range after the array had been sort. This function can be
* helpful to proof the completeness of data.
* $arr = array( 3, 5, 4, 6, 7, 9, 8 );
* if( Arrays::integratedAscending( $arr, 3, 9 ) )
* echo "This will surely be output.";
* if( Arrays::integratedAscending( $arr, 3, 7 ) )
* echo "This will NOT be output.";
* $arr2 = array( 3, 5, 7, 9, 8 );
* if( Arrays::integratedAscending( $arr2, 3, 9 ) )
* echo "This will also NOT be output.";
* @param array $array The array to be probed.
* @param integer $rangeA The minimum of the range.
* @param integer $rangeB The maximum of the range.
|| $array[count( $array ) - 1] > $tmpmax
for ( $i = 0; $i < count( $array ); $i++ )
* Returns a string containing a string representation of all the array
* elements on index of the arrays second dimension in the same order, with
* the glue string between each element.
* @param array $array The array which elements will be used.
* @param integer/string $index
* @param boolean $empty_entries Decides wether empty entries
* should add to the string or not.
function implodeIndex( $glue, &$array, $index, $empty_entries = false )
{ $out = $array[$k][$index]; }
empty( $array[$k++ ][$index] )
for ( $i = $k; $i < count( $array ); $i++ )
|| !empty( $array[$i][$index] )
{ $out .= $glue. $array[$i][$index]; }
* Sorts an array by a given second index. You can affect the order of
* character and strings by defining the constnand ALPHABET and
* NO_CASE_ALPHABET in the language file which can be found in the package
* core/lang_spec_values. With defining of the constant LANG, you can switch
* between the various orders on every new page request.
* @param array $array The array that should be sort.
* @param string $index The index or rather name of the second dimension
* @param string $func The suffix of the custum function which should be
* used (See also the function list of the package
* - boolcmp: To sort boolean values ascending. First cames
* - boolcmp_desc: To sort boolean values descending. First
* - charcmp: To sort character values ascending.
* The order can be dedicated by defining the
* constant ALPHABET in the language files that
* can be found in the package
* - charcmp_desc: To sort character values descending.
* The order can be dedicated by defining the
* constant ALPHABET in the language files that
* can be found in the package
* - intcmp: To sort integer values ascending.
* - intcmp_desc To sort integer values descending.
* - strcmp: To sort Strings ascending.
* The order can be dedicated by defining the
* constant ALPHABET in the language files that
* can be found in the package
* - strcmp_desc: To sort Strings descending.
* The order can be dedicated by defining the
* constant ALPHABET in the language files that
* can be found in the package
$GLOBALS["KEY2SORT"] = $index;
usort( $array, "Arrays__". $func );
////////////////////////////////////////////////////////////////////////////////
* @see http://www.php.net/manual/en/function.usort.php
* @author gk at lka dot hu
* @author Daniel Plücken <daniel@debakel.net>
return $ap < $bp ? - 1 : 1;
* @see http://www.php.net/manual/en/function.usort.php
* @author gk at lka dot hu
* @author Daniel Plücken <daniel@debakel.net>
return $ap < $bp ? 1 : - 1;
* @see http://www.php.net/manual/en/function.usort.php
* @author gk at lka dot hu
* @author Daniel Plücken <daniel@debakel.net>
$tmp_a = strtolower( $astring[$GLOBALS["KEY2SORT"]] );
$tmp_b = strtolower( $bstring[$GLOBALS["KEY2SORT"]] );
$i < strlen( $astring[$GLOBALS["KEY2SORT"]] )
&& $i < strlen( $bstring[$GLOBALS["KEY2SORT"]] )
&& $tmp_a[$i] == $tmp_b[$i];
//if the two strings are the same, the shorter wins
if( $tmp_a[$i] == $tmp_b[$i] )
return strlen( $astring[$GLOBALS["KEY2SORT"]] )
> strlen( $bstring[$GLOBALS["KEY2SORT"]] )
//otherwise depends on the first different char
if( $astring[$GLOBALS["KEY2SORT"]] == $bstring[$GLOBALS["KEY2SORT"]] )
$i < strlen( $astring[$GLOBALS["KEY2SORT"]] )
&& $i < strlen( $bstring[$GLOBALS["KEY2SORT"]] )
&& $astring[$i] == $bstring[$i];
//if the two strings are the same, the shorter wins
$astring[ $GLOBALS["KEY2SORT"] ][$i]
== $bstring[ $GLOBALS["KEY2SORT"] ][$i]
return strlen( $astring[$GLOBALS["KEY2SORT"]] )
> strlen( $bstring[$GLOBALS["KEY2SORT"]] )
//otherwise depends on the first different char
$astring[ $GLOBALS["KEY2SORT"] ][$i],
$bstring[ $GLOBALS["KEY2SORT"] ][$i]
* @see http://www.php.net/manual/en/function.usort.php
* @author gk at lka dot hu
* @author Daniel Plücken <daniel@debakel.net>
$tmp_a = strtolower( $astring[$GLOBALS["KEY2SORT"]] );
$tmp_b = strtolower( $bstring[$GLOBALS["KEY2SORT"]] );
$i < strlen( $astring[$GLOBALS["KEY2SORT"]] )
&& $i < strlen( $bstring[$GLOBALS["KEY2SORT"]] )
&& $tmp_a[$i] == $tmp_b[$i];
//if the two strings are the same, the shorter wins
if( $tmp_a[$i] == $tmp_b[$i] )
return strlen( $astring[$GLOBALS["KEY2SORT"]] )
> strlen( $bstring[$GLOBALS["KEY2SORT"]] )
//otherwise depends on the first different char
if( $astring[$GLOBALS["KEY2SORT"]] == $bstring[$GLOBALS["KEY2SORT"]] )
$i < strlen( $astring[$GLOBALS["KEY2SORT"]] )
&& $i < strlen( $bstring[$GLOBALS["KEY2SORT"]] )
&& $astring[$i] == $bstring[$i];
//if the two strings are the same, the shorter wins
$astring[ $GLOBALS["KEY2SORT"] ][$i]
== $bstring[ $GLOBALS["KEY2SORT"] ][$i]
return strlen( $astring[$GLOBALS["KEY2SORT"]] )
> strlen( $bstring[$GLOBALS["KEY2SORT"]] )
//otherwise depends on the first different char
$astring[ $GLOBALS["KEY2SORT"] ][$i],
$bstring[ $GLOBALS["KEY2SORT"] ][$i]
* Custom sort algorithm to sort integer.
* @author Daniel Plücken <daniel@debakel.net>
if( $a[$GLOBALS["KEY2SORT"]] == $b[$GLOBALS["KEY2SORT"]] )
return $a[$GLOBALS["KEY2SORT"]] < $b[$GLOBALS["KEY2SORT"]] ? - 1 : 1;
* Custom sort algorithm to sort integer.
* @author Daniel Plücken <daniel@debakel.net>
if( $a[$GLOBALS["KEY2SORT"]] == $b[$GLOBALS["KEY2SORT"]] )
return $a[$GLOBALS["KEY2SORT"]] < $b[$GLOBALS["KEY2SORT"]] ? 1 : - 1;
* Custom sort algorithm to sort booleans. First false then true.
* @author Daniel Plücken <daniel@debakel.net>
$a[$GLOBALS["KEY2SORT"]] && $b[$GLOBALS["KEY2SORT"]]
|| !$a[$GLOBALS["KEY2SORT"]] && !$b[$GLOBALS["KEY2SORT"]]
if( $a[$GLOBALS["KEY2SORT"]] )
* Custom sort algorithm to sort booleans. First true then false.
* @author Daniel Plücken <daniel@debakel.net>
$a[$GLOBALS["KEY2SORT"]] && $b[$GLOBALS["KEY2SORT"]]
|| !$a[$GLOBALS["KEY2SORT"]] && !$b[$GLOBALS["KEY2SORT"]]
if( $a[$GLOBALS["KEY2SORT"]] )
|