萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP二維數組怎麼按某個字段排序?

PHP二維數組怎麼按某個字段排序?

   先不說別的我們直接看二維數組按某個字段排序實例

 代碼如下   <?php    
    /**  
     * @author yebihai http://www.45it.com
     * @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<$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)<$sort_total){    
copyright © 萬盛學電腦網 all rights reserved