note 91511 deleted from function.sort by danbrown

From: Date: Sun, 14 Jun 2009 14:12:45 +0000
Subject: note 91511 deleted from function.sort by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156849@lists.php.net to get a copy of this message
Note Submitter: maca dot pavel at gmail dot cz ---- <?php /** * Unisort function * * Returns input array sorted by custom order. Order for every symbol is set by array $customization. If symbol isn't in array $customization, function add this symbol to end ordered by value get from function bin2hex() * * @Copyright Pavel Máca (C) 2009 * @Author Pavel Máca <maca.pavel@gmail.cz> * @license GPL/GNU * @param array $input Array to by sorted. * @param array $customization sorted symbols by custom order. * @param boolean $uniksort - When set TRUE INPUT will by sort by keys. Deufault is FALSE witch will sort INPUT by values. * @param boolean $reverse reverse INPUT witch array_reverse() function. Default is FALSE. * @param boolean $encoding The encoding parameter is the character encoding. If it is omitted, the internal character encoding value will be used. * @return array */ function unisort(array $input, array $customization, $uniksort=false, $reverse=false, $encoding=false ){ if($encoding==false){ $encoding=mb_internal_encoding(); } $max=0; foreach($input as $key=>$val){ if($uniksort==true){ if($max < mb_strlen($key, $encoding)){ $max= mb_strlen($key,$encoding); } } else{ if($max < mb_strlen($val, $encoding)){$max= mb_strlen($val,$encoding); } } } foreach($input as $key=>$val){ if($uniksort==true){ $sort=$key; } else { $sort=$val; } $size[$key]=""; for($i=0; $i<=$max; $i++){ $znak= mb_strtolower(mb_substr($sort, $i, 1, $encoding), $encoding); if($znak!=""){ if(in_array($znak, $customization)){ $num=array_search($znak, $customization); } else{ $num=count($customization) + bin2hex($znak); } } else { $num=0; } $size[$key].= $num.','; } } natsort($size); $array=array(); foreach($size as $key=>$x){ $array[$key]= $input[$key]; } if($reverse==true){ $array = array_reverse($array);} return $array; } ?> // Sample for czech alfabet $custom_order=array('.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'á', 'a', 'b', 'c', 'č', 'd', 'ď', 'e', 'é', 'ě', 'f', 'g', 'h', 'í', 'i', 'j', 'k', 'l', 'm', 'n', 'ň', 'ó', 'o', 'p', 'q', 'r', 'ř', 's', 'š', 't', 'ť', 'ú', 'ů', 'u', 'v', 'w', 'x', 'ý', 'y', 'z', 'ž'); $array=array("rok2009"=>"řepa", "rok2010"=>"redukce", "-neznamý"=>"štěk", "."=>"štamgasti" ); //original array print_r($array); /* Array ( [rok2009] => řepa [rok2010] => redukce [-neznamý] => štěk [.] => štamgasti ) */ //order by values print_r(unisort($array, $custom_order)); /* Array ( [rok2010] => redukce [rok2009] => řepa [.] => štamgasti [-neznamý] => štěk ) */ //order by keys print_r(unisort($array, $custom_order, true)); /* Array ( [.] => štamgasti [rok2009] => řepa [rok2010] => redukce [-neznamý] => štěk ) */

« previous php.notes (#156849) next »