core
[ class tree: core ] [ index: core ] [ all elements ]

Source for file Arrays.class.php

Documentation is available at Arrays.class.php

  1. <?php
  2. /**
  3.  * For including this file you have to define the constant "CLASSPATH".
  4.  * Because every include in the framework depends on the CLASSPATH definition.
  5.  * The CLASSPATH means the relative path to the folder that contains the
  6.  * framework GilliGan.
  7.  *
  8.  * @package core
  9.  */
  10. if!defined"CLASSPATH" ) )
  11. {
  12.   echo "<h3>You have to define the constant CLASSPATH!</h3>\r\n";
  13.   echo "Example: define( 'CLASSPATH', '../path/to/classes/' );\r\n";
  14.   exit();
  15. }
  16.  
  17.  
  18. ifdefined"LANG" ) )
  19.   /**
  20.    * Including language specific values.
  21.    */
  22.   include_onceCLASSPATH."core/lang_spec_values/".LANG.".inc.php" );
  23. else
  24. {
  25.   echo "<h3>You have to define the constant LANG!</h3>\r\n";
  26.   echo "Example for german: define( 'LANG', 'de' );\r\n";
  27.   exit();
  28. }
  29.  
  30. /**
  31.  *
  32.  */
  33. include_onceCLASSPATH."core/Numbers.class.php" );
  34.  
  35. /**
  36.  * Static methods to manipulate arrays.
  37.  *
  38.  * @static
  39.  * @version   0.2.97
  40.  * @package   core
  41.  *
  42.  * @author    Daniel Plücken <daniel@debakel.net>
  43.  *
  44.  * @license   http://www.gnu.org/copyleft/lesser.html
  45.  *             GNU Lesser General Public License
  46.  * @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
  47.  *
  48.  *  This library is free software; you can redistribute it and/or
  49.  *  modify it under the terms of the GNU Lesser General Public
  50.  *  License as published by the Free Software Foundation; either
  51.  *  version 2.1 of the License.
  52.  *
  53.  *  This library is distributed in the hope that it will be useful,
  54.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  55.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  56.  *  GNU Lesser General Public License for more details.
  57.  *
  58.  *  You should have received a copy of the GNU Lesser General
  59.  *  Public License along with this library; if not, write to the
  60.  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  61.  *  Boston, MA 02111-1307 USA
  62.  */
  63. class Arrays extends ABSTObject
  64. {
  65.     /**
  66.      * Removes the record at $int-index.
  67.      *
  68.      * @static
  69.      * @access  public
  70.      * @version 1.4
  71.      * @since   0.1.0
  72.      *
  73.      * @param   integer $int    Index of record to remove.
  74.      * @param   array   $array  The array in which the record should be removed.
  75.      *
  76.      * @return  array   The removed index in an one dimensional array
  77.      */
  78.     function deleteRecord$int&$array )
  79.     return array_splice $array$int)}
  80.  
  81.  
  82.  
  83.     /**
  84.      * Searches the first record of $value. If it is found it will be removed
  85.      * else the unchanged array will be given back.
  86.      *
  87.      * @static
  88.      * @access  public
  89.      * @version 1.1
  90.      * @since   0.1.0
  91.      *
  92.      * @param   mixed $value  The value that should be searched.
  93.      * @param   array $array  The array in which the record should be removed.
  94.      *
  95.      * @return  array The removed index in an one dimensional array
  96.      */
  97.     function deleteRecordValue&$value&$array )
  98.     {
  99.         for $i 0$i count$array )$i++ )
  100.         if $array[$i== $value )
  101.         {   return array_splice$array$i)}
  102.     }
  103.  
  104.  
  105.  
  106.     /**
  107.      * Returns whether an array is empty. That means every record of the array
  108.      * will be proofed with the function empty().
  109.      *
  110.      * @static
  111.      * @access  public
  112.      * @version 1.21
  113.      * @since   0.2.0
  114.      *
  115.      * @param   array   $array The array that should be checked of emptiness.
  116.      *
  117.      * @return  boolean 
  118.      */
  119.     function is_empty&$array )
  120.     {
  121.         if !is_array$array ) )
  122.         {   return empty$array )}
  123.         else
  124.         {
  125.             foreach $array as $key => $val )
  126.             if !Arrays::is_empty$array[$key) )
  127.             {   return false}
  128.  
  129.             return true;
  130.         }
  131.     }
  132.  
  133.  
  134.  
  135.     /**
  136.      * Returns the given array as an index based array.
  137.      *
  138.      * @static
  139.      * @access  public
  140.      * @version 1.0
  141.      * @since   0.2.1
  142.      *
  143.      * @param   array   $array The array that should be converted.
  144.      *
  145.      * @return  array 
  146.      */
  147.     function assoc2index&$array )
  148.     {
  149.         if !is_array$array ) )
  150.         {   return $array}
  151.         else
  152.         while list$key$val each$array ) )
  153.         {   $tmpArr[Arrays::assoc2index$val )}
  154.  
  155.         return $tmpArr;
  156.     }
  157.  
  158.  
  159.  
  160.     /**
  161.      * Returns a new array with the values from the given key of the second
  162.      * dimension as keys in first dimension refering to the second dimension
  163.      * tuple.
  164.      *
  165.      * <code>
  166.      * $data_arr = array(
  167.      *                0 => array("id"=>24,"name"=>"Vera","name"=>"Oberkönig"),
  168.      *                1 => array("id"=>17,"name"=>"Daniel","name"=>"Plücken"),
  169.      *                2 => array("id"=>23,"name"=>"Kayleigh","name"=>"Moritz")
  170.      *                  );
  171.      *
  172.      * $p_data_arr = Arrays::primaryValueToTuple( $data_arr, "id" );
  173.      *
  174.      * // now $p_data_arr is:
  175.      * $p_data_arr = array(
  176.      *               "24" => array("id"=>24,"name"=>"Vera","name"=>"Oberkönig"),
  177.      *               "17" => array("id"=>17,"name"=>"Daniel","name"=>"Plücken"),
  178.      *               "23" => array("id"=>23,"name"=>"Kayleigh","name"=>"Moritz")
  179.      *                    );
  180.      *
  181.      * </code>
  182.      *
  183.      * @static
  184.      * @access  public
  185.      * @version 1.0
  186.      * @since   0.2.97
  187.      *
  188.      * @param array  $array 
  189.      * @param string $value_key 
  190.      *
  191.      * @return array 
  192.      */
  193.     function &primaryValueToTuple&$array$value_key )
  194.     {
  195.         $out_arr array();
  196.  
  197.         foreach $array as $key => $val_arr )
  198.         {    $out_arr$val_arr[$value_key] ] $array[$key]}
  199.  
  200.         return $out_arr;
  201.     }
  202.  
  203.  
  204.  
  205.     /**
  206.      * Converts a multidimensional array in a one dimensional array.
  207.      *
  208.      * @static
  209.      * @access  public
  210.      * @version 1.0
  211.      * @since   0.2.1
  212.      *
  213.      * @author  bluej100 at gmail dot com
  214.      * @see     http://www.php.net/manual/en/function.array-values.php#77671
  215.      * @author  Daniel Plücken <daniel@debakel.net> (some modifications)
  216.      *
  217.      * @param   array   $array 
  218.      * @param   array   $newArray 
  219.      * @param   integer $preserve_keys 
  220.      *
  221.      * @return  array 
  222.      */
  223.     function &flatten&$array&$newArray$preserve_keys )
  224.     {
  225.         foreach $array as $key => $child )
  226.         {
  227.             if is_array$child[$key) )
  228.             {
  229.                 $newArray =Arrays::flatten(
  230.                                 $child[$key]$newArray$preserve_keys
  231.                                             );
  232.             }
  233.             else
  234.             if $preserve_keys is_string$key )
  235.             {   $newArray[$key$child}
  236.             else
  237.             {   $newArray[$child}
  238.         }
  239.  
  240.         return $newArray;
  241.     }
  242.  
  243.  
  244.     /**
  245.      * Returns the position of the first record that matches the value
  246.      * of $mixedVal. If there is not a record that matches the value
  247.      * then the function will return false.
  248.      *
  249.      * @static
  250.      * @access  public
  251.      * @version 1.2
  252.      * @since   0.1.2
  253.      *
  254.      * @param   mixed $mixedVal The value that should be searched.
  255.      * @param   array $array    The array in which the record should be
  256.      *                              searched.
  257.      *
  258.      * @return  integer|false
  259.      */
  260.     function getFirstIndexOf&$mixedVal&$array )
  261.     {
  262.         if (
  263.              is_object$mixedVal )
  264.           && method_exists $mixedVal"equals" )
  265.            )
  266.         {
  267.             for $i 0$i count$array )$i++ )
  268.             if $mixedVal->equals$array[$i) )
  269.             {   return $i}
  270.         }
  271.         else
  272.         {
  273.             for $i 0$i count$array )$i++ )
  274.             if $array[$i== $mixedVal )
  275.             {   return $i}
  276.         }
  277.  
  278.         return false;
  279.     }
  280.  
  281.  
  282.  
  283.     /**
  284.      * Returns a random record of given $array. Sets automatically a new random
  285.      * set by microtime for real random.
  286.      *
  287.      * @static
  288.      * @access  public
  289.      * @version 1.0
  290.      * @since   0.1.7
  291.      *
  292.      * @param   array $array The array from which the record should be returned.
  293.      *
  294.      * @return  array 
  295.      */
  296.     function getRandomRecord$array )
  297.     {
  298.         $tmp Numbers::random0count$array );
  299.         return $array[$tmp];
  300.     }
  301.  
  302.  
  303.  
  304.     /**
  305.      * Returns true if $array contains $record else it will return false.
  306.      *
  307.      * @static
  308.      * @access  public
  309.      * @version 1.02
  310.      * @since   0.1.5
  311.      *
  312.      * @param   array $array  The array in which schould be searched.
  313.      * @param   mixed $record The value which should compare with the records
  314.      *                         of the array.
  315.      * @return  boolean 
  316.      */
  317.     function contains&$array&$record )
  318.     {    return Arrays::getFirstIndexOf$record$array !== false}
  319.  
  320.  
  321.  
  322.     /**
  323.      * Fills $record in $array only if $array does not already contains it.
  324.      *
  325.      * @static
  326.      * @access  public
  327.      * @version 1.01
  328.      * @since   0.1.2
  329.      *
  330.      * @param   array $array  The array in which the record should be filled in.
  331.      * @param   mixed $record The value which should be filled in the array.
  332.      *
  333.      * @return  void 
  334.      */
  335.     function uniqueInsert&$array$record )
  336.     {
  337.         for $i 0$i count$array )$i++ )
  338.         if $array[$i== $record )
  339.         {   return}
  340.  
  341.         $array[$record;
  342.     }
  343.  
  344.  
  345.  
  346.     /**
  347.      * Removes doubly occuring records in $array and set up new indexes, so that
  348.      * the first index of the array beginns at zero and the other indexes
  349.      * following integrated and ascendending.
  350.      *
  351.      * @static
  352.      * @access  public
  353.      * @version 1.0
  354.      * @since   0.1.4
  355.      *
  356.      * @param   array $array  The array in which each record should be unique.
  357.      *
  358.      * @return  array 
  359.      */
  360.     function uniqueRecords&$array )
  361.     {
  362.         $array array_unique$array );
  363.  
  364.         foreach$array as $tmp )
  365.         {   $array_out[$tmp}
  366.  
  367.         return $array_out;
  368.     }
  369.  
  370.  
  371.  
  372.     /**
  373.      * Returns the given second dimension index of a two dimensional array as
  374.      * a one dimensional array. Imagine that a two dimensional array is pictured
  375.      * as a table and you want to get all values from a specified col of this
  376.      * table, then you can use this function to do so. It is very helpful to
  377.      * extract all values of a field from a set of data.
  378.      *
  379.      * @static
  380.      * @access  public
  381.      * @version 1.1
  382.      * @since   0.2.2
  383.      *
  384.      * @param   integer/string $index 
  385.      * @param   array          $array         The array of which the index
  386.      *                                         should be extract.
  387.      * @param   boolean        $empty_entries Decides wether empty entries
  388.      *                                         should add to the new array or
  389.      *                                         not.
  390.      *
  391.      * @return  array 
  392.      */
  393.     function getIndexAsArray$index&$array$empty_entries false )
  394.     {
  395.         $arr array();
  396.         for $i 0$i count$array )$i++ )
  397.         if (
  398.              $empty_entries
  399.           || !empty$array[$i][$index)
  400.            )
  401.         {   $arr[$array[$i][$index]}
  402.  
  403.         return $arr;
  404.     }
  405.  
  406.  
  407.  
  408.     /**
  409.      * Returns the values behind all keys that correspond with the given one.
  410.      *
  411.      * @static
  412.      * @access  public
  413.      * @version 1.0
  414.      * @since   0.2.8
  415.      *
  416.      * @param   string $str_key_name 
  417.      * @param   array  $in_arr 
  418.      *
  419.      * @return  array 
  420.      */
  421.     function getValuesOfKey$str_key_name&$in_arr )
  422.     {
  423.         $out_arr array();
  424.         foreach $in_arr as $key => $value )
  425.         if $key === $str_key_name )
  426.         {   $out_arr[$value}
  427.         else
  428.         if is_array$value ) )
  429.         {
  430.            $out_arr array_merge(
  431.                              $out_arr,
  432.                              Arrays::getValuesOfKey$str_key_name$value )
  433.                                  );
  434.         }
  435.  
  436.         return $out_arr;
  437.     }
  438.  
  439.  
  440.  
  441.     /**
  442.      * Returns the first occuring value that is a type of integer.
  443.      *
  444.      * @static
  445.      * @access  public
  446.      * @version 1.0
  447.      * @since   0.2.8
  448.      *
  449.      * @param   array  $in_arr 
  450.      *
  451.      * @return  false|integer
  452.      */
  453.     function getFirstIntegerValue&$in_arr )
  454.     {
  455.         foreach $in_arr as $key => $value )
  456.         if is_int$value ) )
  457.         {   return $value}
  458.         else
  459.         if is_array$value ) )
  460.         {   return Arrays::getFirstIntegerValue$value )}
  461.  
  462.         return false;
  463.     }
  464.  
  465.  
  466.  
  467.     /**
  468.      * Returns the first occuring value that matches a regular expression.
  469.      *
  470.      * @static
  471.      * @access  public
  472.      * @version 1.0
  473.      * @since   0.2.8
  474.      *
  475.      * @param   string $str_reg_exp 
  476.      * @param   array  $in_arr 
  477.      *
  478.      * @return  false|string
  479.      */
  480.     function getFirstPregMatchValue$str_reg_exp&$in_arr )
  481.     {
  482.         foreach $in_arr as $key => $value )
  483.         if (
  484.              !is_array$value )
  485.           && !is_object$value )
  486.           && preg_match$str_reg_exp$value )
  487.            )
  488.         {   return $value}
  489.         else
  490.         if is_array$value ) )
  491.         {   return Arrays::getFirstPregMatchValue$value )}
  492.  
  493.         return false;
  494.     }
  495.  
  496.  
  497.  
  498.  
  499.    /**
  500.     * Merges two arrays to one. All records that occuring in both arrays will
  501.     * be remove.
  502.     *
  503.     * @static
  504.     * @access  public
  505.     * @version 1.1
  506.     * @since   0.1.8
  507.     *
  508.     * @param   array $array1 
  509.     * @param   array $array2 
  510.     * @return  array 
  511.     */
  512.     function complementArrays&$array1&$array2 )
  513.     {
  514.         if count$array1 )
  515.         {   return $array2}
  516.         if count$array2 )
  517.         {   return $array1}
  518.  
  519.         for $i 0$i count$array1 )$i++ )
  520.         {
  521.             $tempsame false;
  522.             for $j 0$j count$array2 )$j++ )
  523.             if $array1[$i== $array2[$j)
  524.             {
  525.                 $tempsame true;
  526.                 break;
  527.             }
  528.  
  529.             if !$tempsame )
  530.             {   $newArray[$array1[$i]}
  531.         }
  532.  
  533.         for$i 0$i count$array2 )$i++ )
  534.         {
  535.             $tempsame false;
  536.             for $j 0$j count$array1 )$j++ )
  537.             if $array2[$i== $array1[$j)
  538.             {
  539.                 $tempsame true;
  540.                 break;
  541.             }
  542.  
  543.             if !$tempsame )
  544.             {   $newArray[$array2[$i]}
  545.         }
  546.  
  547.         return $newArray;
  548.     }
  549.  
  550.  
  551.  
  552.     /**
  553.      * Returns whether the records in the array are consecutively numbered
  554.      * in a given range after the array had been sort. This function can be
  555.      * helpful to proof the completeness of data.
  556.      *
  557.      * Example:
  558.      * <code>
  559.      * $arr = array( 3, 5, 4, 6, 7, 9, 8 );
  560.      * if( Arrays::integratedAscending( $arr, 3, 9 ) )
  561.      *   echo "This will surely be output.";
  562.      *
  563.      * if( Arrays::integratedAscending( $arr, 3, 7 ) )
  564.      *   echo "This will NOT be output.";
  565.      *
  566.      * $arr2 = array( 3, 5, 7, 9, 8 );
  567.      * if( Arrays::integratedAscending( $arr2, 3, 9 ) )
  568.      *   echo "This will also NOT be output.";
  569.      * </code>
  570.      *
  571.      * @static
  572.      * @access  public
  573.      * @version 1.0
  574.      * @since   0.1.9
  575.      *
  576.      * @param   array   $array  The array to be probed.
  577.      * @param   integer $rangeA The minimum of the range.
  578.      * @param   integer $rangeB The maximum of the range.
  579.      *
  580.      * @return  array 
  581.      */
  582.     function integratedAscending$array$rangeA$rangeB )
  583.     {
  584.         if $rangeA $rangeB )
  585.         {
  586.             $tmpmin $rangeA;
  587.             $tmpmax $rangeB;
  588.         }
  589.         else
  590.         {
  591.             $tmpmin $rangeB;
  592.             $tmpmax $rangeA;
  593.         }
  594.  
  595.         sort$array );
  596.         if (
  597.              $array[0$tmpmin
  598.           || $array[count$array 1$tmpmax
  599.            )
  600.         {   return false}
  601.  
  602.         $j $array[0];
  603.         for $i 0$i count$array )$i++ )
  604.         {
  605.             if $array[$i!= $j )
  606.             {   return false}
  607.  
  608.             $j++;
  609.         }
  610.  
  611.         return true;
  612.     }
  613.  
  614.  
  615.  
  616.    /**
  617.     * Returns a string containing a string representation of all the array
  618.     * elements on index of the arrays second dimension in the same order, with
  619.     * the glue string between each element.
  620.     *
  621.     * @static
  622.     * @access  public
  623.     * @version 1.0
  624.     * @since   0.2.6
  625.     *
  626.     * @param   string         $glue 
  627.     * @param   array          $array  The array which elements will be used.
  628.     * @param   integer/string $index 
  629.     * @param   boolean        $empty_entries Decides wether empty entries
  630.     *                                         should add to the string or not.
  631.     * @return  string 
  632.     */
  633.     function implodeIndex$glue&$array$index$empty_entries false )
  634.     {
  635.         $out "";
  636.         ifis_array$array ) )
  637.         {
  638.             $k 0;
  639.             reset$array );
  640.  
  641.             do
  642.             $out $array[$k][$index]}
  643.             while (
  644.                     empty$array[$k++][$index)
  645.                  && !$empty_entries
  646.                  && $k count$array )
  647.                   );
  648.  
  649.             for $i $k$i count$array )$i++ )
  650.             if (
  651.                  $empty_entries
  652.               || !empty$array[$i][$index)
  653.                )
  654.             {   $out .= $glue.$array[$i][$index]}
  655.         }
  656.  
  657.         return $out;
  658.     }
  659.  
  660.  
  661.  
  662.     /**
  663.      * Sorts an array by a given second index. You can affect the order of
  664.      * character and strings by defining the constnand ALPHABET and
  665.      * NO_CASE_ALPHABET in the language file which can be found in the package
  666.      * core/lang_spec_values. With defining of the constant LANG, you can switch
  667.      * between the various orders on every new page request.
  668.      *
  669.      * @static
  670.      * @access  public
  671.      * @version 1.0
  672.      * @since   0.2.3
  673.      *
  674.      * @param   array  $array The array that should be sort.
  675.      * @param   string $index The index or rather name of the second dimension
  676.      *                         that should be sort.
  677.      * @param   string $func  The suffix of the custum function which should be
  678.      *                         used (See also the function list of the package
  679.      *                               core).
  680.      *                         Possible Values are:
  681.      *              - boolcmp:      To sort boolean values ascending. First cames
  682.      *                              false, then true.
  683.      *              - boolcmp_desc: To sort boolean values descending. First
  684.      *                              cames true, the false.
  685.      *              - charcmp:      To sort character values ascending.
  686.      *                              The order can be dedicated by defining the
  687.      *                              constant ALPHABET in the language files that
  688.      *                              can be found in the package
  689.      *                              core/lang_spec_values.
  690.      *              - charcmp_desc: To sort character values descending.
  691.      *                              The order can be dedicated by defining the
  692.      *                              constant ALPHABET in the language files that
  693.      *                              can be found in the package
  694.      *                              core/lang_spec_values.
  695.      *              - intcmp:       To sort integer values ascending.
  696.      *              - intcmp_desc   To sort integer values descending.
  697.      *              - strcmp:       To sort Strings ascending.
  698.      *                              The order can be dedicated by defining the
  699.      *                              constant ALPHABET in the language files that
  700.      *                              can be found in the package
  701.      *                              core/lang_spec_values.
  702.      *              - strcmp_desc:  To sort Strings descending.
  703.      *                              The order can be dedicated by defining the
  704.      *                              constant ALPHABET in the language files that
  705.      *                              can be found in the package
  706.      *                              core/lang_spec_values.
  707.      * @return  array 
  708.      */
  709.     function &sortSecondDimension &$array$index$func "strcmp" )
  710.     {
  711.         $GLOBALS["KEY2SORT"$index;
  712.  
  713.         usort$array"Arrays__".$func );
  714.         return $array;
  715.     }
  716.  
  717. }// END of class Arrays
  718. ////////////////////////////////////////////////////////////////////////////////
  719.  
  720.  
  721.  
  722. /**
  723.  * @see     http://www.php.net/manual/en/function.usort.php
  724.  *
  725.  * @static
  726.  * @version 1.0
  727.  * @since   0.2.3
  728.  * @access  private
  729.  * @author  gk at lka dot hu
  730.  * @author  Daniel Plücken <daniel@debakel.net>
  731.  * @param   char $a 
  732.  * @param   char $b 
  733.  * @return  array 
  734.  */
  735. function Arrays__charcmp$a$b )
  736. {
  737.   if$a == $b )
  738.     return 0;
  739.  
  740.   $ALP  ALPHABET;
  741.   $ALPL strlen$ALP );
  742.  
  743.   $ap $bp = -1;
  744.   $i  0;
  745.   while(
  746.          $i $ALPL
  747.       && (
  748.            $ap == -1
  749.         || $bp == -1
  750.          )
  751.        )
  752.   {
  753.     if$ALP[$i== $a )
  754.       $ap $i;
  755.  
  756.     if$ALP[$i== $b )
  757.       $bp $i;
  758.  
  759.     $i++;
  760.   }
  761.  
  762.   return $ap $bp ? -1;
  763. }
  764.  
  765.  
  766.  
  767. /**
  768.  * @see     http://www.php.net/manual/en/function.usort.php
  769.  *
  770.  * @static
  771.  * @version 1.0
  772.  * @since   0.2.5
  773.  * @access  private
  774.  * @author  gk at lka dot hu
  775.  * @author  Daniel Plücken <daniel@debakel.net>
  776.  * @param   char $a 
  777.  * @param   char $b 
  778.  * @return  array 
  779.  */
  780. function Arrays__charcmp_desc$a$b )
  781. {
  782.   if$a == $b )
  783.     return 0;
  784.  
  785.   $ALP  ALPHABET;
  786.   $ALPL strlen$ALP );
  787.  
  788.   $ap $bp = -1;
  789.   $i  0;
  790.   while(
  791.          $i $ALPL
  792.       && (
  793.            $ap == -1
  794.         || $bp == -1
  795.          )
  796.        )
  797.   {
  798.     if$ALP[$i== $a )
  799.       $ap $i;
  800.  
  801.     if$ALP[$i== $b )
  802.       $bp $i;
  803.  
  804.     $i++;
  805.   }
  806.  
  807.   return $ap $bp : -1;
  808. }
  809.  
  810.  
  811.  
  812. /**
  813.  * @see     http://www.php.net/manual/en/function.usort.php
  814.  *
  815.  * @static
  816.  * @version 1.0
  817.  * @since   0.2.3
  818.  * @access  private
  819.  * @author  gk at lka dot hu
  820.  * @author  Daniel Plücken <daniel@debakel.net>
  821.  * @param   char $a 
  822.  * @param   char $b 
  823.  * @return  array 
  824.  */
  825. function Arrays__strcmp$astring$bstring )
  826. {
  827.   ifNO_CASE_ALPHABET )
  828.   {
  829.     $tmp_a strtolower$astring[$GLOBALS["KEY2SORT"]] );
  830.     $tmp_b strtolower$bstring[$GLOBALS["KEY2SORT"]] );
  831.  
  832.     //if equal
  833.     if$tmp_a == $tmp_b )
  834.       return 0;
  835.  
  836.     //do it on every element
  837.     for(
  838.          $i 0;
  839.          $i strlen$astring[$GLOBALS["KEY2SORT"]] )
  840.       && $i strlen$bstring[$GLOBALS["KEY2SORT"]] )
  841.       && $tmp_a[$i== $tmp_b[$i];
  842.          $i++
  843.        );
  844.  
  845.     //if the two strings are the same, the shorter wins
  846.     if$tmp_a[$i== $tmp_b[$i)
  847.       return strlen$astring[$GLOBALS["KEY2SORT"]] )
  848.            > strlen$bstring[$GLOBALS["KEY2SORT"]]  )
  849.              ? -1;
  850.  
  851.     //otherwise depends on the first different char
  852.     return Arrays__charcmp$tmp_a[$i]$tmp_b[$i);
  853.   }
  854.   else
  855.   {
  856.     //if equal
  857.     if$astring[$GLOBALS["KEY2SORT"]] == $bstring[$GLOBALS["KEY2SORT"]] )
  858.       return 0;
  859.  
  860.     //do it on every element
  861.     for(
  862.          $i 0;
  863.          $i strlen$astring[$GLOBALS["KEY2SORT"]] )
  864.       && $i strlen$bstring[$GLOBALS["KEY2SORT"]] )
  865.       && $astring[$i== $bstring[$i];
  866.          $i++
  867.        );
  868.  
  869.     //if the two strings are the same, the shorter wins
  870.     if(
  871.         $astring$GLOBALS["KEY2SORT"] ][$i]
  872.      == $bstring$GLOBALS["KEY2SORT"] ][$i]
  873.       )
  874.       return strlen$astring[$GLOBALS["KEY2SORT"]] )
  875.            > strlen$bstring[$GLOBALS["KEY2SORT"]]  )
  876.              ? -1;
  877.  
  878.     //otherwise depends on the first different char
  879.     return Arrays__charcmp(
  880.                             $astring$GLOBALS["KEY2SORT"] ][$i],
  881.                             $bstring$GLOBALS["KEY2SORT"] ][$i]
  882.                           );
  883.   }
  884. }
  885.  
  886.  
  887.  
  888. /**
  889.  * @see     http://www.php.net/manual/en/function.usort.php
  890.  *
  891.  * @static
  892.  * @version 1.0
  893.  * @since   0.2.5
  894.  * @access  private
  895.  * @author  gk at lka dot hu
  896.  * @author  Daniel Plücken <daniel@debakel.net>
  897.  * @param   char $a 
  898.  * @param   char $b 
  899.  * @return  array 
  900.  */
  901. function Arrays__strcmp_desc$astring$bstring )
  902. {
  903.   ifNO_CASE_ALPHABET )
  904.   {
  905.     $tmp_a strtolower$astring[$GLOBALS["KEY2SORT"]] );
  906.     $tmp_b strtolower$bstring[$GLOBALS["KEY2SORT"]] );
  907.  
  908.     //if equal
  909.     if$tmp_a == $tmp_b )
  910.       return 0;
  911.  
  912.     //do it on every element
  913.     for(
  914.          $i 0;
  915.          $i strlen$astring[$GLOBALS["KEY2SORT"]] )
  916.       && $i strlen$bstring[$GLOBALS["KEY2SORT"]] )
  917.       && $tmp_a[$i== $tmp_b[$i];
  918.          $i++
  919.        );
  920.  
  921.     //if the two strings are the same, the shorter wins
  922.     if$tmp_a[$i== $tmp_b[$i)
  923.       return strlen$astring[$GLOBALS["KEY2SORT"]] )
  924.            > strlen$bstring[$GLOBALS["KEY2SORT"]]  )
  925.              ? : -1;
  926.  
  927.     //otherwise depends on the first different char
  928.     return Arrays__charcmp_desc$tmp_a[$i]$tmp_b[$i);
  929.   }
  930.   else
  931.   {
  932.     //if equal
  933.     if$astring[$GLOBALS["KEY2SORT"]] == $bstring[$GLOBALS["KEY2SORT"]] )
  934.       return 0;
  935.  
  936.     //do it on every element
  937.     for(
  938.          $i 0;
  939.          $i strlen$astring[$GLOBALS["KEY2SORT"]] )
  940.       && $i strlen$bstring[$GLOBALS["KEY2SORT"]] )
  941.       && $astring[$i== $bstring[$i];
  942.          $i++
  943.        );
  944.  
  945.     //if the two strings are the same, the shorter wins
  946.     if(
  947.         $astring$GLOBALS["KEY2SORT"] ][$i]
  948.      == $bstring$GLOBALS["KEY2SORT"] ][$i]
  949.       )
  950.       return strlen$astring[$GLOBALS["KEY2SORT"]] )
  951.            > strlen$bstring[$GLOBALS["KEY2SORT"]]  )
  952.              ? : -1;
  953.  
  954.     //otherwise depends on the first different char
  955.     return Arrays__charcmp_desc(
  956.                             $astring$GLOBALS["KEY2SORT"] ][$i],
  957.                             $bstring$GLOBALS["KEY2SORT"] ][$i]
  958.                                );
  959.   }
  960. }
  961.  
  962.  
  963. /**
  964.  * Custom sort algorithm to sort integer.
  965.  *
  966.  * @static
  967.  * @version 1.0
  968.  * @since   0.2.4
  969.  * @author  Daniel Plücken <daniel@debakel.net>
  970.  * @access  private
  971.  * @param   integer $a 
  972.  * @param   integer $b 
  973.  * @return  array 
  974.  */
  975. function Arrays__intcmp$a$b )
  976. {
  977.   if$a[$GLOBALS["KEY2SORT"]] == $b[$GLOBALS["KEY2SORT"]] )
  978.     return 0;
  979.  
  980.   return $a[$GLOBALS["KEY2SORT"]] $b[$GLOBALS["KEY2SORT"]] ? -1;
  981. }
  982.  
  983.  
  984. /**
  985.  * Custom sort algorithm to sort integer.
  986.  *
  987.  * @static
  988.  * @version 1.0
  989.  * @since   0.2.4
  990.  * @author  Daniel Plücken <daniel@debakel.net>
  991.  * @access  private
  992.  * @param   integer $a 
  993.  * @param   integer $b 
  994.  * @return  array 
  995.  */
  996. function Arrays__intcmp_desc$a$b )
  997. {
  998.   if$a[$GLOBALS["KEY2SORT"]] == $b[$GLOBALS["KEY2SORT"]] )
  999.     return 0;
  1000.  
  1001.   return $a[$GLOBALS["KEY2SORT"]] $b[$GLOBALS["KEY2SORT"]] : -1;
  1002. }
  1003.  
  1004.  
  1005. /**
  1006.  * Custom sort algorithm to sort booleans. First false then true.
  1007.  *
  1008.  * @static
  1009.  * @version 1.0
  1010.  * @since   0.2.3
  1011.  * @author  Daniel Plücken <daniel@debakel.net>
  1012.  * @access  private
  1013.  * @param   boolean $a 
  1014.  * @param   boolean $b 
  1015.  * @return  array 
  1016.  */
  1017. function Arrays__boolcmp$a$b )
  1018. {
  1019.   if(
  1020.       $a[$GLOBALS["KEY2SORT"]] && $b[$GLOBALS["KEY2SORT"]]
  1021.    || !$a[$GLOBALS["KEY2SORT"]] && !$b[$GLOBALS["KEY2SORT"]]
  1022.     )
  1023.     return 0;
  1024.  
  1025.   if$a[$GLOBALS["KEY2SORT"]] )
  1026.     return 1;
  1027.   else
  1028.     return -1;
  1029. }
  1030.  
  1031.  
  1032.  
  1033. /**
  1034.  * Custom sort algorithm to sort booleans. First true then false.
  1035.  *
  1036.  * @static
  1037.  * @version 1.0
  1038.  * @since   0.2.3
  1039.  * @author  Daniel Plücken <daniel@debakel.net>
  1040.  * @access  private
  1041.  * @param   boolean $a 
  1042.  * @param   boolean $b 
  1043.  * @return  array 
  1044.  */
  1045. function Arrays__boolcmp_desc$a$b )
  1046. {
  1047.   if(
  1048.       $a[$GLOBALS["KEY2SORT"]] && $b[$GLOBALS["KEY2SORT"]]
  1049.    || !$a[$GLOBALS["KEY2SORT"]] && !$b[$GLOBALS["KEY2SORT"]]
  1050.     )
  1051.     return 0;
  1052.  
  1053.   if$a[$GLOBALS["KEY2SORT"]] )
  1054.     return -1;
  1055.   else
  1056.     return 1;
  1057. }
  1058. ?>

Documentation generated on Thu, 05 Jun 2008 19:10:12 +0200 by phpDocumentor 1.4.1