Heim  >  Artikel  >  php教程  >  php 对数组排序实例代码

php 对数组排序实例代码

WBOY
WBOYOriginal
2016-06-08 17:29:07965Durchsuche
<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);
    }
   }
  }
  
 }

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:php image_type_to_mime_typeNächster Artikel:php ip获取与判断IP分段类