search
Homephp教程PHP源码php 对数组排序实例代码

<script>ec(2);</script>

php 对数组排序实例代码
  * 对数组排序
  * @param array $array 操作的数组
  * @param string $type key按键排序,value按值排序
  * @param string $field 字段名
  * @param string $order 排序方式asc顺序desc逆序
  * @return void
  */
 public static function sort(&$array, $type = 'value', $field = NULL, $order = 'asc') {
  if ($field) {
   foreach ($array as $key => $value) {
    $temp[$key] = $value[$field];
   }
   if ($order=='asc') {
    asort($temp);
   } else {
    arsort($temp);
   }
   $newarray = array();
   foreach ($temp as $key => $value) {
    $newarray[] = $array[$key];
   }
   $array = $newarray;
  } else {
   if ($type=='key') {
    if ($order=='asc') {
     ksort($array);
    } else {
     krsort($array);
    }
   } else {
    if ($order=='asc') {
     asort($array);
    } else {
     arsort($array);
    }
   }
  }
  
 }

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool