note 91908 deleted from function.sort by danbrown
| From: | danbrown@php.net | Date: | Wed, 01 Jul 2009 12:48:44 +0000 |
| Subject: | note 91908 deleted from function.sort by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157770@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 sequence. OUTPUT is array sorted according to array
$customization. If symbol isn't in array $customization, function add this symbol to end
ordered by value get from function bin2hex(). It can by used to sort all symbols in any language.
*
* @Copyright Pavel Máca (C) 2009
* @Author Pavel Máca <maca.pavel@gmail.cz>
* @license GPL/GNU
* @param array $input - The input array.
* @param array $customization - It contains the symbols sorted to custom order.
* @param boolean $uniksort - When set TRUE INPUT will by sort by keys. Deufault is FALSE which will
rank INPUT according to values.
* @param boolean $reverse - Reverse INPUT with 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;
}
?>
//Example
$customization=array('a', 'e', 'i', 'o', 'u',
'b', 'c', 'd');
$input=array('beta', 'demo', 'error', 'time',
'alfa', 'battle');
print_r(unisort($input, $customization));
Array
(
[4] => alfa
[2] => error
[5] => battle
[0] => beta
[1] => demo
[3] => time
)
//Example of uniksort
$customization=array('a', 'e', 'i', 'o', 'u',
'b', 'c', 'd');
$input=array('beta'=>1, 'demo'=>2, 'error'=>3,
'time'=>4, 'alfa'=>5, 'battle'=>6);
$output=unisort($input, $customization, true);
print_r($output);
Array
(
[alfa] => 5
[error] => 3
[battle] => 6
[beta] => 1
[demo] => 2
[time] => 4
)
//Example of the Czech alphabet
$encoding='UTF-8'; // Document is in UTF-8.
$customization=array('a', 'á', 'b', 'c',
'Ä', 'd', 'r', 'Å');
$input=array('náÅez', 'nakopat', 'cukr', 'deka',
'Äokoláda', 'ÅeÅicha', 'ropa',
'buben','dárek');
print_r(unisort($input, $customization, false, false, 'utf-8'));
Array
(
[7] => buben
[2] => cukr
[4] => Äokoláda
[8] => dárek
[3] => deka
[6] => ropa
[5] => ÅeÅicha
[1] => nakopat
[0] => náÅez
)