Home > php教程 > php手册 > body text

PHP二维数组排序简单实现方法,php二维数组排序

WBOY
Release: 2016-06-13 08:46:21
Original
1154 people have browsed it

PHP二维数组排序简单实现方法,php二维数组排序

本文实例讲述了PHP二维数组排序简单实现方法。分享给大家供大家参考,具体如下:

function multi_compare($a, $b)
{
  $val_arr = array(
      'gold'=>'asc',
      'silver'=>'desc'//还可以增加额外的排序条件
  );
  foreach($val_arr as $key => $val){
    if($a[$key] == $b[$key]){
      continue;
    }
    return (($val == 'desc')&#63;-1:1) * (($a[$key] < $b[$key]) &#63; -1 : 1);
  }
  return 0;
}
$arr = array(
  array('gold'=>1, 'silver'=>2),
  array('gold'=>8, 'silver'=>10),
  array('gold'=>8, 'silver'=>8),
  array('gold'=>2, 'silver'=>1),
);
uasort($arr, 'multi_compare');
print_r($arr);

Copy after login

运行结果如下:

Array
(
  [0] => Array
    (
      [gold] => 1
      [silver] => 2
    )
  [3] => Array
    (
      [gold] => 2
      [silver] => 1
    )
  [1] => Array
    (
      [gold] => 8
      [silver] => 10
    )
  [2] => Array
    (
      [gold] => 8
      [silver] => 8
    )
)

Copy after login

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php排序算法总结》、《PHP基本语法入门教程》、《PHP错误与异常处理方法总结》及《php常用函数与技巧总结》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • php中二维数组排序问题方法详解
  • php中array_multisort对多维数组排序的方法
  • php专用数组排序类ArraySortUtil用法实例
  • php通过sort()函数给数组排序的方法
  • PHP实现指定字段的多维数组排序函数分享
  • php选择排序法实现数组排序实例分析
  • php插入排序法实现数组排序实例
  • php数组排序usort、uksort与sort函数用法
  • PHP数组排序之sort、asort与ksort用法实例
  • php实例分享之二维数组排序
  • PHP二维数组排序的3种方法和自定义函数分享
  • 一个PHP二维数组排序的函数分享
  • php二维数组排序方法(array_multisort usort)
  • 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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!