首頁  >  文章  >  後端開發  >  共享一个PHP对二维数组的某个字段进展升降排序类

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

WBOY
WBOY原創
2016-06-13 10:41:21811瀏覽

共享一个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;    }}?>
?

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn