Home Backend Development PHP Tutorial php对二维数组按指定键值key排序示例代码_php技巧

php对二维数组按指定键值key排序示例代码_php技巧

May 17, 2016 am 08:52 AM
Two-dimensional array

复制代码 代码如下:

function array_sort($array, $key){
if(is_array($array)){
$key_array = null;
$new_array = null;
for( $i = 0; $i $key_array[$array[$i][$key]] = $i;
}
ksort($key_array);
$j = 0;
foreach($key_array as $k => $v){
$new_array[$j] = $array[$v];
$j++;
}
unset($key_array);
return $new_array;
}else{
return $array;
}
}

PHP二维数组按键值排序

PHP 中 array_multisort() 可以用来一次对多个数组进行排序,或者根据某一维或多维对多维数组进行排序。关联键名保持不变,但数字键名会被重新索引。输入数组被当成一个表的列并以行来排序,第一个数组是要排序的主要数组。数组中的行(值)比较为相同的话就按照下一个输入数组中相应值的大小来排序,依此类推。

但是如果需要排序的数组是一个二维数组,需要按照数组的键值排序,如下面这个二维数组,需要按照sort键名来排序,那么array_multisort() 就无法直接实现了:

$data[5] = array('volume' => 67, 'edition' => 2);
$data[4] = array('volume' => 86, 'edition' => 1);
$data[2] = array('volume' => 85, 'edition' => 6);
$data[3] = array('volume' => 98, 'edition' => 2);
$data[1] = array('volume' => 86, 'edition' => 6);
$data[6] = array('volume' => 67, 'edition' => 7);
// 准备要排序的数组
foreach ($data as $k => $v) {
  $edition[] = $v['edition'];
}
array_multisort($edition, SORT_ASC, $data);
print_r($data);
Copy after login

将输出:

Array
(
  [0] => Array
    (
      [volume] => 86
      [edition] => 1
    )

  [1] => Array
    (
      [volume] => 67
      [edition] => 2
    )

  [2] => Array
    (
      [volume] => 98
      [edition] => 2
    )

  [3] => Array
    (
      [volume] => 85
      [edition] => 6
    )

  [4] => Array
    (
      [volume] => 86
      [edition] => 6
    )

  [5] => Array
    (
      [volume] => 67
      [edition] => 7
    )

)
Copy after login

为了不破坏原有的key,写了一个排序函数,仅支持二维数组。

/**
* 根据数组中的某个键值大小进行排序,仅支持二维数组
* 
* @param array $array 排序数组
* @param string $key 键值
* @param bool $asc 默认正序
* @return array 排序后数组
*/
function arraySortByKey(array $array, $key, $asc = true) 
{
  $result = array();
  // 整理出准备排序的数组
  foreach ( $array as $k => &$v ) {
    $values[$k] = isset($v[$key]) ? $v[$key] : '';
  }
  unset($v);
  // 对需要排序键值进行排序
  $asc ? asort($values) : arsort($values);
  // 重新排列原有数组
  foreach ( $values as $k => $v ) {
    $result[$k] = $array[$k];
  }
 
  return $result;
}
Copy after login
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use the array_sum function in PHP to calculate the sum of elements in a column of a two-dimensional array How to use the array_sum function in PHP to calculate the sum of elements in a column of a two-dimensional array Jun 26, 2023 pm 12:45 PM

How to use the array_sum function in PHP to calculate the sum of elements in a column of a two-dimensional array

How to convert a two-dimensional php array into a one-dimensional array How to convert a two-dimensional php array into a one-dimensional array Aug 03, 2023 am 11:14 AM

How to convert a two-dimensional php array into a one-dimensional array

How to get the value of a specified column in a two-dimensional array using the array_column function in PHP How to get the value of a specified column in a two-dimensional array using the array_column function in PHP Jun 26, 2023 pm 01:32 PM

How to get the value of a specified column in a two-dimensional array using the array_column function in PHP

How to reverse a two-dimensional array in php How to reverse a two-dimensional array in php Dec 26, 2022 am 09:38 AM

How to reverse a two-dimensional array in php

How to convert 2D array to 1D array in PHP How to convert 2D array to 1D array in PHP Jul 07, 2023 pm 06:42 PM

How to convert 2D array to 1D array in PHP

Does php have two-dimensional arrays? Does php have two-dimensional arrays? Aug 03, 2023 pm 02:45 PM

Does php have two-dimensional arrays?

Detailed explanation of PHP 5.5 functions: How to use the array_column function to extract a certain column in a two-dimensional array Detailed explanation of PHP 5.5 functions: How to use the array_column function to extract a certain column in a two-dimensional array Jul 30, 2023 am 08:45 AM

Detailed explanation of PHP 5.5 functions: How to use the array_column function to extract a certain column in a two-dimensional array

In C programming, working with 2D arrays at runtime In C programming, working with 2D arrays at runtime Sep 13, 2023 pm 11:29 PM

In C programming, working with 2D arrays at runtime

See all articles