Home >Backend Development >PHP Tutorial >PHP method to sort a certain key value of a multidimensional array_PHP tutorial

PHP method to sort a certain key value of a multidimensional array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:30895browse

php method for sorting a certain key value of a multi-dimensional array

PHP multi-dimensional array sorting can be processed with the array_multisort() function. The array_multisort() function can be used to sort multiple items at once. Arrays are sorted, or multi-dimensional arrays are sorted according to a certain dimension or multiple dimensions. The associated (string) key names remain unchanged, but the numeric key names will be re-indexed

Sort order flag:

SORT_ASC - Sort in ascending order

SORT_DESC - Sort in descending order

Sorting type flag:

SORT_REGULAR - Compare items in the usual way

SORT_NUMERIC - Compare items numerically

SORT_STRING - Compare items by string

Example 1. Sort multiple arrays

The code is as follows

$ar1 = array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);

代码如下

$ar1 = array("10", 100, 100, "a");
$ar2 = array(1, 3, "2", 1);
array_multisort($ar1, $ar2);

var_dump($ar1);
var_dump($ar2);
?>

var_dump($ar1);
var_dump($ar2);
?>

In this example, after sorting, the first array will contain "10", "a", 100, 100. The second array will contain 1,1,"2",3. The order of the items in the second array is exactly the same as the order of the corresponding items (100 and 100) in the first array.

But none of these can specify which key value to rank. Below I have compiled a few functions and algorithms, all of which are good.

Sample code
 代码如下  

/**
 * 对多位数组进行排序
 * @param $multi_array 数组
 * @param $sort_key需要传入的键名
 * @param $sort排序类型
 */
function multi_array_sort($multi_array, $sort_key, $sort = SORT_DESC) {
if (is_array($multi_array)) {
foreach ($multi_array as $row_array) {
if (is_array($row_array)) {
$key_array[] = $row_array[$sort_key];
} else {
return FALSE;
}
}
} else {
return FALSE;
}
array_multisort($key_array, $sort, $multi_array);
return $multi_array;
}

The code is as follows

/**
* Sort multi-digit array
* @param $multi_array array
* @param $sort_key The key name to be passed
* @param $sort sorting type
​*/
function multi_array_sort($multi_array, $sort_key, $sort = SORT_DESC) {
if (is_array($multi_array)) {
foreach ($multi_array as $row_array) {
if (is_array($row_array)) {
$key_array[] = $row_array[$sort_key];
} else {
return FALSE;
}
}
} else {
return FALSE;
}
array_multisort($key_array, $sort, $multi_array);
return $multi_array;
}

  例子二

 代码如下  

 function array_sort($arr, $keys, $type = 'asc') {

 代码如下  

 function array_sort($arr, $keys, $type = 'asc') {

$keysvalue = $new_array = array();

foreach ($arr as $k => $v) {

$keysvalue[$k] = $v[$keys];
}

if ($type == 'asc') {

asort($keysvalue);
} else {

arsort($keysvalue);
}

reset($keysvalue);

foreach ($keysvalue as $k => $v) {

$new_array[] = $arr[$k];
}

return $new_array;
}

$keysvalue = $new_array = array();

foreach ($arr as $k => $v) {

$keysvalue[$k] = $v[$keys];
}

if ($type == 'asc') {

asort($keysvalue);
} else {

arsort($keysvalue);
}

reset($keysvalue);

foreach ($keysvalue as $k => $v) {

$new_array[] = $arr[$k];
}

return $new_array;
}

  例子3

 代码如下  

/*
function:二维数组按指定的键值排序
author:www.111cn.net
*/
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;
}

代码如下

/*
function:二维数组按指定的键值排序
author:www.111cn.net
*/
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;
}

  例如有下列这样一个数组:

 代码如下  

$array=array(
0=>array('id'=>8,'username'=>'phpernote'),
1=>array('id'=>9,'username'=>'com'),
2=>array('id'=>5,'username'=>'www')
);

 代码如下  

$array=array(
0=>array('id'=>8,'username'=>'phpernote'),
1=>array('id'=>9,'username'=>'com'),
2=>array('id'=>5,'username'=>'www')
);

现在需要将这个二维数组按id升序排列,则:

array_sort($array,'id','asc');

现在需要将这个二维数组按id升序排列,则:

array_sort($array,'id','asc');

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/834975.htmlTechArticlephp method for sorting a certain key value of a multi-dimensional array. PHP multi-dimensional array sorting can be processed with the array_multisort() function. The array_multisort() function can be used to sort multiple arrays at once,...
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