note 91511 added to function.sort

From: Date: Sun, 14 Jun 2009 12:43:51 +0000
Subject: note 91511 added to function.sort
Groups: php.notes 
Request: Send a blank email to php-notes+get-156848@lists.php.net to get a copy of this message
<?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 ) */ ---- Server IP: 147.32.160.17 Probable Submitter: 82.100.0.210 ---- Manual Page -- http://www.php.net/manual/en/function.sort.php Edit -- https://master.php.net/note/edit/91511 Del: integrated -- https://master.php.net/note/delete/91511/integrated Del: useless -- https://master.php.net/note/delete/91511/useless Del: bad code -- https://master.php.net/note/delete/91511/bad+code Del: spam -- https://master.php.net/note/delete/91511/spam Del: non-english -- https://master.php.net/note/delete/91511/non-english Del: in docs -- https://master.php.net/note/delete/91511/in+docs Del: other reasons-- https://master.php.net/note/delete/91511 Reject -- https://master.php.net/note/reject/91511 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156848) next »