PHP
·
发表于 5年以前
·
阅读量:8296
±¾ÎÄʵÀý½²ÊoÁËPHPʵÏÖµÄ×Ô¶¨ÒaÊý×eÅÅÐoº¯ÊýÓeÅÅÐoÀa¡£*ÖÏi¸ø´o¼Ò¹(C)´o¼Ò²Î¿¼£¬¾ßÌaÈçÏ£º
/*
* ¶þάÊý×e×Ô¶¨ÒaÅÅÐoº¯Êý
* uasort($arr,function_name)
*
**/
$arr = array(
array('a'=>1,'b'=>'c'),
array('a'=>4,'b'=>'a'),
array('a'=>5,'b'=>'g'),
array('a'=>7,'b'=>'f'),
array('a'=>6,'b'=>'e')
);
function compare_arr($x,$y){
if($x['b']<$y['b']){
return -1;
}else if($x['b']>$y['b']){
return 1;
}else{
return 0;
}
}
uasort($arr,'compare_arr');
foreach($arr as $a){
echo $a['a'].'=>'.$a['b'].'<br/>';
}
ÊÖ²aÀiµÄ×Ô¶¨ÒaÅÅÐoÀa£º
class multiSort
{
var $key; //key in your array
//ÅÅÐoº¯Êý ²ÎÊýÒÀ´ÎÊÇ Êý×e ´ýÅÅÁÐË÷Òý ÅÅÐoÀaÐÍ
function run ($myarray, $key_to_sort, $type_of_sort = '')
{
$this->key = $key_to_sort;
if ($type_of_sort == 'desc')
uasort($myarray, array($this, 'myreverse_compare'));
else
uasort($myarray, array($this, 'mycompare'));
return $myarray;
}
//ÕýÐo
function mycompare($x, $y)
{
if ( $x[$this->key] == $y[$this->key] )
return 0;
else if ( $x[$this->key] < $y[$this->key] )
return -1;
else
return 1;
}
//ÄaeÐo
function myreverse_compare($x, $y)
{
if ( $x[$this->key] == $y[$this->key] )
return 0;
else if ( $x[$this->key] > $y[$this->key] )
return -1;
else
return 1;
}
}
¸u¶a¹ØÓÚPHPÏa¹ØÄÚÈݸÐÐËȤµÄ¶ÁÕ߿ɲe¿´±¾Õ¾×¨Ìa£º¡¶PHPÊý×e(Array)²Ù×÷¼¼ÇÉ´oÈ«¡¡¢¡¶[phpÅÅÐoËa¨×ܽa](http://www.jb51.net/Special/150.htm)¡¡¢¡¶[php×Öu´®(string)Óè×ܽa](http://www.jb51.net/Special/47.htm)¡¡¢¡¶PHPÕe¶ÔXMLÎļþ²Ù×÷¼¼ÇÉ×ܽa¡¡¢¡¶[PHP´iÎoÓeÒi³£´¦Ài½¨×ܽa](http://www.jb51.net/Special/196.htm)¡¡¢¡¶PHPÔËËaÓeÔËËauÓè×ܽa¡¡¢¡¶[PHP»u±¾Ói¨ÈeÃŽ̳Ì](http://www.jb51.net/Special/348.htm)¡¡¢¡¶phpÃaeÏo¶ÔÏo³ÌÐoÉe¼ÆÈeÃŽ̡̳¡¢¡¶php+mysqlÊý¾Ý¿a²Ù×÷ÈeÃŽ̡̳¼°¡¶php³£¼uÊý¾Ý¿a²Ù×÷¼¼ÇÉ»a×Ü¡
Ï£Íu±¾ÎÄËuÊo¶Ô´o¼ÒPHP³ÌÐoÉe¼ÆÓÐËu°iÖu¡£