服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - PHP多维数组排序array详解

PHP多维数组排序array详解

2021-07-18 17:49Stephen102 PHP教程

这篇文章主要为大家详细介绍了PHP多维数组排序array的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

PHP数组Array按字段排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * 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按二维排序,先按第一个字段排序,再按第二个字段排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 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])

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
   * 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;
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/u010571844/article/details/51284317

延伸 · 阅读

精彩推荐