Home  >  Article  >  Backend Development  >  共享一个PHP对二维数组的某个字段进展升降排序类

共享一个PHP对二维数组的某个字段进展升降排序类

WBOY
WBOYOriginal
2016-06-13 10:41:21811browse

共享一个PHP对二维数组的某个字段进行升降排序类

<?php/** * @author paul <[email&#160;protected]> * @desc 对二维数组的某一个字段进行升降排序 * @data * 		$testData = array(			array('newPrice'=>9),			array('newPrice'=>21),			array('newPrice'=>15),			array('newPrice'=>13),			array('newPrice'=>12)		); */class sortClass{    //升序	function sortArrayAsc($preData,$sortType='newPrice'){        $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){                    $sortData2 = array_slice($sortData, $min_key-1);                     foreach ($sortData2 as $value){                        array_push($sortData1, $value);                    }                }                $sortData = $sortData1;            }        }        return $sortData;    }	//降序	function sortArrayDesc($preData,$sortType='newPrice'){        $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){                    $sortData2 = array_slice($sortData, $min_key-1);                     foreach ($sortData2 as $value){                        array_push($sortData1, $value);                    }                }                $sortData = $sortData1;            }        }        return $sortData;    }}?>
?

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
Previous article:phpexcel 读取xls资料Next article:PHP兑现各种排序