Home >php教程 >php手册 >PHP二维数组怎么按某个字段排序?

PHP二维数组怎么按某个字段排序?

WBOY
WBOYOriginal
2016-06-13 09:51:291379browse

数据排序有很多现成的函数,但是如果想对指定字段进行排序就没有内置函数了,下面我整理了一些相关的数组排序函数与大家一起交流学习学习。

先不说别的我们直接看二维数组按某个字段排序实例

 代码如下 复制代码
    /**  
     * @author yebihai http://www.bKjia.c0m
     * @desc 按二维数组的某一个字段进行升降排序  
     * @data  
     *      $testData = array(  
                array('price'=>19),  
                array('price'=>121),  
                array('price'=>115),  
                array('price'=>113),  
                array('price'=>112)  
            );  
     */    
    class sortClass{    
        //升序    
        function sortArrayAsc($preData,$sortType='price'){    
            $sortData = array();    
            foreach ($preData as $key_i => $value_i){    
                $price_i = $value_i[$sortType];    
                $min_key = '';    
                $sort_total = count($sortData);    
                foreach ($sortData as $key_j => $value_j){    
                    if($price_i                         $min_key = $key_j+1;    
                        break;    
                    }    
                }    
                if(empty($min_key)){  
                    array_push($sortData, $value_i);     
                }else {    
                    $sortData1 = array_slice($sortData, 0,$min_key-1);     
                    array_push($sortData1, $value_i);    
                    if(($min_key-1)                         $sortData2 = array_slice($sortData, $min_key-1);     
                        foreach ($sortData2 as $value){    
                            array_push($sortData1, $value);    
                        }    
                    }    
                    $sortData = $sortData1;    
                }    
            }    
            return $sortData;    
        }    
        //降序    
        function sortArrayDesc($preData,$sortType='price'){    
            $sortData = array();    
            foreach ($preData as $key_i => $value_i){    
                $price_i = $value_i[$sortType];    
                $min_key = '';    
                $sort_total = count($sortData);    
                foreach ($sortData as $key_j => $value_j){    
                    if($price_i>$value_j[$sortType]){    
                        $min_key = $key_j+1;    
                        break;    
                    }    
                }    
                if(empty($min_key)){    
                    array_push($sortData, $value_i);     
                }else {    
                    $sortData1 = array_slice($sortData, 0,$min_key-1);     
                    array_push($sortData1, $value_i);    
                    if(($min_key-1)                         $sortData2 = array_slice($sortData, $min_key-1);     
                        foreach ($sortData2 as $value){    
                            array_push($sortData1, $value);    
                        }    
                    }    
                    $sortData = $sortData1;    
                }    
            }    
            return $sortData;    
        }    
    }    

后面整理一些关于数组排序的函数

数据排序函数有

•sort() 函数用于对数组单元从低到高进行排序。
•rsort() 函数用于对数组单元从高到低进行排序。
•asort() 函数用于对数组单元从低到高进行排序并保持索引关系。
•arsort() 函数用于对数组单元从高到低进行排序并保持索引关系。
•ksort() 函数用于对数组单元按照键名从低到高进行排序。
•krsort() 函数用于对数组单元按照键名从高到低进行排序。
array_multisort() 函数对多个数组或多维数组进行排序

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn