Home  >  Article  >  php教程  >  根据数据长度对数组排序

根据数据长度对数组排序

PHP中文网
PHP中文网Original
2016-05-25 17:09:511107browse

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

                   

                   

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