首页 > 后端开发 > php教程 > PHP多维数组排序array详解

PHP多维数组排序array详解

jacklove
发布: 2023-03-27 15:00:02
原创
1958 人浏览过

本篇讲解了PHP多维数组排序array详解。

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&#39; => SORT_DESC])
2. sortMultiArray($data, [‘score&#39; => SORT_DESC, ‘count&#39; => SORT_ASC])
3. sortMultiArray($data, [‘score&#39; => SORT_DESC, ‘count&#39; => SORT_ASC, ‘name&#39; => 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;
 
}
登录后复制

本篇讲解了PHP多维数组排序array详解,更多相关内容请关注php中文网。

相关推荐:

PHP实现桶排序算法

PHP排序算法系列之归并排序详解

thinkPHP5框架数据库连贯操作:cache()用法详情

以上是PHP多维数组排序array详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板