頻道欄目
首頁 > 資訊 > php > 正文

PHP多維數組排序array詳解

18-01-05        來源:[db:作者]  
收藏   我要投稿
PHP數組Array按字段排序
/**

* Sort array by filed and type, common utility method.

* @param array $data

* @param string $sort_filed

* @param string $sort_type SORT_ASC or SORT_DESC

*/

public function sortByOneField($data, $filed, $type)

{

if (count($data) <= 0) {

return $data;

}

foreach ($data as $key => $value) {

$temp[$key] = $value[$filed];

}

array_multisort($temp, $type, $data);

return $data;

}

PHP數組Array按二維排序,先按第一個字段排序,再按第二個字段排序

/**

* Sort array by filed and type, common utility method.

* @param array $array

* @param string $filed1

* @param string $type1 SORT_ASC or SORT_DESC

* @param string $filed2

* @param string $type2 SORT_ASC or SORT_DESC

*/

public function sortByTwoFiled($data, $filed1, $type1, $filed2, $type2)

{

if (count($data) <= 0) {

return $data;

}

foreach ($data as $key => $value) {

$temp_array1[$key] = $value[$filed1];

$temp_array2[$key] = $value[$filed2];

}

array_multisort($temp_array1, $type1, $temp_array2, $type2, $data);

return $users;

}

sortMultiArray()最多支持3維數組排序,當然可以擴充的,自定義方法重載實現對多維數字的排序,這里的多維是說數據的多個字段。

使用方法:

1. sortMultiArray($data, [‘score' => SORT_DESC])

2. sortMultiArray($data, [‘score' => SORT_DESC, ‘count' => SORT_ASC])

3. sortMultiArray($data, [‘score' => SORT_DESC, ‘count' => SORT_ASC, ‘name' => SORT_ASC])

/**

* Sort multi array by filed and type.

* @param data $array

* @param condition $array

*/

public function sortMultiArray(&$data, $condition)

{

if (count($data) <= 0 || empty($condition)) {

return $data;

}

$dimension = count($condition);

$fileds = array_keys($condition);

$types = array_values($condition);

switch ($dimension) {

case 1:

$data = $this->sort1Dimension($data, $fileds[0], $types[0]);

break;

case 2:

$data = $this->sort2Dimension($data, $fileds[0], $types[0], $fileds[1], $types[1]);

break;

default:

$data = $this->sort3Dimension($data, $fileds[0], $types[0], $fileds[1], $types[1], $fileds[2], $types[2]);

break;

}

return $data;

}

public function sort1Dimension(&$data, $filed, $type)

{

if (count($data) <= 0) {

return $data;

}

foreach ($data as $key => $value) {

$temp[$key] = $value[$filed];

}

array_multisort($temp, $type, $data);

return $data;

}

public function sort2Dimension(&$data, $filed1, $type1, $filed2, $type2)

{

if (count($data) <= 0) {

return $data;

}

foreach ($data as $key => $value) {

$sort_filed1[$key] = $value[$filed1];

$sort_filed2[$key] = $value[$filed2];

}

array_multisort($sort_filed1, $type1, $sort_filed2, $type2, $data);

return $data;

}

public function sort3Dimension(&$data, $filed1, $type1, $filed2, $type2, $filed3, $type3)

{

if (count($data) <= 0) {

return $data;

}

foreach ($data as $key => $value) {

$sort_filed1[$key] = $value[$filed1];

$sort_filed2[$key] = $value[$filed2];

$sort_filed3[$key] = $value[$filed3];

}

array_multisort($sort_filed1, $type1, $sort_filed2, $type2, $sort_filed3, $type3, $data);

return $data;

}

 

相關TAG標簽
上一篇:php語言最牛掰的Laravel框架詳情
下一篇:最全的PHP正則表達式(手機號需要加上177-***)詳情
相關文章
圖文推薦

關于我們 | 聯系我們 | 廣告服務 | 投資合作 | 版權申明 | 在線幫助 | 網站地圖 | 作品發布 | Vip技術培訓 | 舉報中心

版權所有: 紅黑聯盟--致力于做實用的IT技術學習網站

美女MM131爽爽爽毛片