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')?-1:1) * (($a[$key] < $b[$key]) ? -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);
运行结果如下:
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 ) )
更多关于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二维数组排序详解

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1
Powerful PHP integrated development environment