search
Homephp教程php手册PHP二维数组排序简单实现方法,php二维数组排序

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);

运行结果如下:

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二维数组排序详解
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool