欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

PHP多维数组排序array详解

程序员文章站 2022-04-10 08:12:27
php数组array按字段排序 /** * sort array by filed and type, common utility method....

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;
  }

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