Home > php教程 > PHP源码 > 根据数据长度对数组排序

根据数据长度对数组排序

PHP中文网
Release: 2016-05-25 17:09:51
Original
1159 people have browsed it

1. [PHP]代码  

function __toArray( $key,$val){
  return array( "name"=>$key,"url"=>$val);
}
/*
  依赖 __toArray();
  @params $sort_arr = array( "北京"=>"http://www.baidu.com","北京12313"=>"http://www.beijing.com")
  根据数组长度 倒序排列
 */
function sortByLen( $sort_arr){
  $length_map = array_map( "strlen",array_keys( $sort_arr));
  $tmp = array_map( "__toArray",array_keys( $sort_arr),array_values( $sort_arr));
  arsort( $length_map);
  $cache =array();
  if( is_valid_array( $length_map)){
    foreach($length_map as $index=>$val){
      $cache[] = $tmp[ $index];
    }
  }
  return $cache;
}
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
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
Latest Articles by Author
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template