PHP two-dimensional array sorted by specified key value_PHP tutorial

WBOY
Release: 2016-07-13 17:00:27
Original
1264 people have browsed it

We have talked about various PHP array sorting methods before. At the same time, PHP also provides a large number of data sorting functions, such as sort(), asort(), arsort(), etc. Let me introduce two-dimensional arrays to you. Method to sort by specified key value.

Specific examples

return $keysvalue; }
The code is as follows
 代码如下 复制代码

/*二维数组按指定的键值排序*/
function array_sort($array,$keys,$type='asc'){
 if(!isset($array) || !is_array($array) || empty($array)){
  return '';
 }
 if(!isset($keys) || trim($keys)==''){
  return '';
 }
 if(!isset($type) || $type=='' || !in_array(strtolower($type),array('asc','desc'))){
  return '';
 }
 $keysvalue=array();
 foreach($array as $key=>$val){
  $val[$keys] = str_replace('-','',$val[$keys]);
  $val[$keys] = str_replace(' ','',$val[$keys]);
  $val[$keys] = str_replace(':','',$val[$keys]);
  $keysvalue[] =$val[$keys];
 }
 asort($keysvalue); //key值排序
 reset($keysvalue); //指针重新指向数组第一个
 foreach($keysvalue as $key=>$vals) {
  $keysort[] = $key;
 }
 $keysvalue = array();
 $count=count($keysort);
 if(strtolower($type) != 'asc'){
  for($i=$count-1; $i>=0; $i--) {
   $keysvalue[] = $array[$keysort[$i]];
  }
 }else{
  for($i=0; $i<$count; $i++){
$keysvalue[] = $array[$keysort[$i]];
}
}
return $keysvalue;
}

Copy code

/*The two-dimensional array is sorted by the specified key value*/

function array_sort($array,$keys,$type='asc'){
代码如下 复制代码
$a=array(
1=>array(
id=>1,
price=>'79',
),
2=>array(
id=>1,
price=>'68',
),
);

if(!isset($array) || !is_array($array) || empty($array)){

return '';

}
 代码如下 复制代码

array_sort($a,'price');

if(!isset($keys) || trim($keys)==''){

return ''; } if(!isset($type) || $type=='' || !in_array(strtolower($type),array('asc','desc'))){

return '';

}
$keysvalue=array();
foreach($array as $key=>$val){
$val[$keys] = str_replace('-','',$val[$keys]);

$val[$keys] = str_replace(' ','',$val[$keys]);

$val[$keys] = str_replace(':','',$val[$keys]);
$keysvalue[] =$val[$keys];
}

asort($keysvalue); //key value sorting

reset($keysvalue); //Redirect the pointer to the first
in the array foreach($keysvalue as $key=>$vals) {
$keysort[] = $key;

}

$keysvalue = array();
$count=count($keysort);
if(strtolower($type) != 'asc'){
for($i=$count-1; $i>=0; $i--) {

$keysvalue[] = $array[$keysort[$i]];

}
}else{
for($i=0; $i<$count; $i++){
$keysvalue[] = $array[$keysort[$i]];
}

}
How to use: For example: Just use:
The code is as follows Copy code
array_sort($a,'price'); php array sort function sort ( &$arr [,fruits] ) Sort the array from low to high and assign a new key name. Return bool bKjia.c0m rsort ( &$arr [,fruits] ) Reverse sort the array and assign new key names asort ( &$arr [,fruits] ) sorts the array, keeping the index unchanged arsort( &$arr [,fruits] ) sorts the array in reverse order and keeps the index unchanged ksort ( &$arr [,fruits] ) sorts the array by key name krsort( &$arr [,fruits] ) sorts the group numbers in reverse order by key name natsort( &$arr ) Performs ‘natural sorting’ on array key values, sorting by length, alphabet, etc. natcasesort( &$arr ) performs case-insensitive ‘natural ranking’ of arrays usort ( &$arr , cmp_function ) User-defined function sorts an array and rearranges key names uksort (&$arr, cmp_function) User-defined function sorts an array by key name uasort (&$arr, cmp_function) user-defined function sorts the array and keeps the index unchanged array_multisort( $arr , mixed) The second parameter is to change the sorting behavior based on the value SORT_REGULAR Normal comparison unit SORT_NUMERIC www.bKjia.c0m The unit is compared as a number SORT_STRING The unit is compared as a string SROT_LOCALE_STRING The unit is compared as a string according to the current local settings

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631255.htmlTechArticleWe have talked about various PHP array sorting methods before, and PHP also provides a large number of data sorting functions. sort(), asort(), arsort(), etc. Let me introduce to you the two-dimensional array index...
Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template