PHP數(shù)組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數(shù)組Array按二維排序,先按第一個(gè)字段排序,再按第二個(gè)字段排序
/**
* 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維數(shù)組排序,當(dāng)然可以擴(kuò)充的,自定義方法重載實(shí)現(xiàn)對(duì)多維數(shù)字的排序,這里的多維是說(shuō)數(shù)據(jù)的多個(gè)字段。
使用方法:
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;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- PHP基于array_unique實(shí)現(xiàn)二維數(shù)組去重
- php數(shù)組函數(shù)array_push()、array_pop()及array_shift()簡(jiǎn)單用法示例
- PHP call_user_func和call_user_func_array函數(shù)的簡(jiǎn)單理解與應(yīng)用分析
- php array 轉(zhuǎn)json及java 轉(zhuǎn)換 json數(shù)據(jù)格式操作示例
- php利用array_search與array_column實(shí)現(xiàn)二維數(shù)組查找
- php使用array_chunk函數(shù)將一個(gè)數(shù)組分割成多個(gè)數(shù)組
- PHP array_reverse() 函數(shù)原理及實(shí)例解析