以前我们有讲过各种php数组排序的方法,同时php也提供了大量的数据排序函数,sort(),asort(),arsort()等,下面我来给各位同学介绍二维数组按指定的键值排序的方法,具体实例代码如下:
- /*二维数组按指定的键值排序*/
- function array_sort($array,$keys,$type='asc'){
- if(!isset($array) || !is_array($array) || emptyempty($array)){
- return '';
- }
- if(!isset($keys) || trim($keys)==''){
- return '';
- }
- if(!isset($type) || $type=='' || !in_array(strtolower($type),array('asc','desc'))){
- return '';
- }
- $keysvalue=array();
- foreach($array as $key=>$val){
- $val[$keys] = str_replace('-','',$val[$keys]);
- $val[$keys] = str_replace(' ','',$val[$keys]);
- $val[$keys] = str_replace(':','',$val[$keys]);
- $keysvalue[] =$val[$keys];
- }
- asort($keysvalue); //key值排序
- reset($keysvalue); //指针重新指向数组第一个
- foreach($keysvalue as $key=>$vals) {
- $keysort[] = $key;
- }
- $keysvalue = array();
- $count=count($keysort);
- if(strtolower($type) != 'asc'){
- for($i=$count-1; $i>=0; $i–) {
- $keysvalue[] = $array[$keysort[$i]];
- }
- }else{
- for($i=0; $i<$count; $i++){
- $keysvalue[] = $array[$keysort[$i]];
- }
- }
- return $keysvalue;
- }
使用方法,代码如下:
- $a=array(
- 1=>array(
- id=>1,
- price=>'79',
- ),
- 2=>array(
- id=>1,
- price=>'68',
- ),
- );
就使用:array_sort($a,'price');
php数组排序函数
sort (&$arr[,fruits]) 对数组进行从低到高排序,并赋予新的键名 返回bool
rsort (&$arr[,fruits]) 对数组进行逆向排序,并赋予新的键名
asort (&$arr[,fruits]) 对数组进行排序,并保持索引不变
arsort(&$arr[,fruits]) 对数组进行逆向排序,并保持索引不变
ksort (&$arr [,fruits]) 对数组按照键名进行排序
krsort(&$arr [,fruits]) 对组数按照键名进行逆向排序
natsort(&$arr) 对数组键值进行‘自然排序法’按照长度,字母排序等
natcasesort(&$arr) 对数组进行不区分大小写的‘自然排名’
usort (&$arr,cmp_function ) 用户自定义函数对一个数组进行排序,重新排列键名
uksort (&$arr,cmp_function ) 用户自定义函数对一个数组进行键名排序
uasort (&$arr,cmp_function) 用户自定义函数对数组进行排序 并保持索引不变
array_multisort($arr,mixed) 第二个参数是可以根据值改变排序行为
SORT_REGULAR 正常比较单元 SORT_NUMERIC 单元被作为数字来比较
SORT_STRING 单元被作为字符串来比较 SROT_LOCALE_STRING 根据当前的local 设置来把单元当做字符串比较
波比源码 » php 二维数组按指定的键值排序
order levofloxacin 250mg online levofloxacin canada
order cialis pills cost cialis 20mg buy sildenafil without prescription
original tadalafil rezeptfrei sicher kaufen sildenafil 200mg kaufen sildenafil 100mg generika
ramipril 10mg without prescription buy generic ramipril azelastine 10ml brand
lasix 40mg pill order doxycycline online plaquenil 200mg without prescription
order chloroquine pills baricitinib medication olumiant 2mg oral
purchase motilium pills motilium over the counter cyclobenzaprine generic
purchase sildalis sale estrace cost lamictal pills
order sildenafil 50mg pills sildenafil 100mg tablets buy cialis for sale
buy generic zithromax prednisolone 20mg cost neurontin 100mg pill
online gambling games planning poker online buy modafinil 200mg online
buy prednisone generic buy prazosin 1mg vermox pill
naprosyn 500mg brand naproxen 500mg oral prevacid cheap
order proventil 100 mcg pill buy proventil for sale ciprofloxacin pills
buy singulair 5mg generic cheap viagra sale viagra 100mg over the counter
parx casino online online casino for real money casino online usa
cost adalat 30mg buy allegra 120mg online fexofenadine 180mg pills
poker online for real money best online poker sites for real money good academic writing
order asacol 800mg generic astelin 10ml brand buy avapro 150mg sale
order acetazolamide generic brand azathioprine 25mg brand imuran
temovate usa buy clobetasol online amiodarone price
order lanoxin sale lanoxin 250 mg uk purchase molnupiravir for sale
alendronate online motrin 400mg ca motrin usa
buy pamelor 25 mg pills paxil 20mg for sale paroxetine medication
indomethacin 75mg over the counter buy indomethacin 50mg capsule buy cenforce
cost modafinil 100mg generic promethazine 25mg phenergan generic
erectile dysfunction pills over the counter buy proscar 1mg pill finasteride 1mg pill
zofran 8mg price cost amoxil 250mg bactrim without prescription
brand accutane 10mg zithromax 250mg us order azithromycin 250mg for sale
purchase avodart for sale purchase xenical for sale orlistat 120mg generic
buy generic azathioprine 25mg order naprosyn 500mg generic order naproxen 250mg for sale