php的array_multisort()使用方法介绍_php基础
使用 array_multisort()函数对多个数组或多维数组进行排序的函数,正在研究数组排序、多维数组排序的朋友可以看看。
函数 bool array_multisort ( array &$arr [, mixed $arg = SORT_ASC [, mixed $arg = SORT_REGULAR [, mixed $...]]] )
参数说明: 函数对多个数组或多维数组进行排序
第一个参数是数组,随后的每一个参数可能是数组,也可能是下面的排序顺序标志
SORT_ASC - 默认,按升序排列
SORT_DESC - 按降序排列
随后可以指定排序的类型
SORT_REGULAR - 默认。将每一项按常规顺序排列。
SORT_NUMERIC - 将每一项按数字顺序排列。
SORT_STRING - 将每一项按字母顺序排列。
例:
$arr1 = array('10', 11, 100, 100, 'a');
$arr2 = array(1, 2, 3, '2', 5);
array_multisort($arr1, $arr2);
?>
结果为:
$arr1
Array ( [0] => 10 [1] => a [2] => 11 [3] => 100 [4] => 100 )
# '10'在与11, 100, 100比较时转换为整数10,小于其他三个数
# '10'在于'a'比较时作为字符串,其第一个字符'1'ascii码值为49小于‘a'(ascii值为97),所以‘10'为最小元素
# 'a'在于其他三个数字比较时,转换为整数0,小于其他三个数
$arr2
Array ( [0] => 1 [1] => 5 [2] => 2 [3] => 2 [4] => 3 )
# $arr2元素1与$arr1元素'10'位置对应,所以排在[0]位置
# $arr1[2] => 100, $arr1[3] => 100分别对应$arr2元素3, '2'。3大于'2',所以与2对应的$arr1[2] => 100排序后的下标为3,与3对应的$arr1[3] => 100排序的下标为4
总结----------
1.参与排序的数组元素数保持一致
2.排序数组元素位置对应如, ‘10' => 1 , 11 => 2
3.后边的数组在前边数组的顺序的基础上进行排序
4.前边的数组如遇相等的元素则比较后边的数组
下边就是使用的实例,手册上的例子不列了,就我自己工作中的几个。
工作中碰到最多的可能就是二维数组了,想试试三维数组,但是想想还是算了。
header('Content-Type: text/html; charset=utf-8');
echo '
'; <br>//原始数组格式 <br>$array = array( <br>'key1' => array( <br>'item1' => '65', <br>'item2' => '35', <br>'item3' => '84', <br>), <br>'key2' => array( <br>'item1' => '24', <br>), <br>'key3' => array( <br>'item1' => '38', <br>'item3' => '45', <br>), <br>); <br>//要排序的键 <br>//按照数组中的 item1进行排序 <br>//你也可以换成item2 <br>$sort = 'item1'; <br>foreach($array as $k => $v) <br>{ <br>$newArr[$k] = $v[$sort]; <br>} <br>//这个函数如果执行正确他会直接改变原数组键值的顺序 <br>//如果执行失败,那么他会返回 bool(false) <br>array_multisort($newArr,SORT_DESC, $array); <br>var_dump($array); <br>//---------------------排序后的数组打印效果 开始-------------------- <br>array(3) { <br>["key1"]=> <br>array(3) { <br>["item1"]=> <br>string(2) "65" <br>["item2"]=> <br>string(2) "35" <br>["item3"]=> <br>string(2) "84" <br>} <br>["key3"]=> <br>array(2) { <br>["item1"]=> <br>string(2) "38" <br>["item3"]=> <br>string(2) "45" <br>} <br>["key2"]=> <br>array(1) { <br>["item1"]=> <br>string(2) "24" <br>} <br>} <br>//---------------------排序后的数组打印效果 结束--------------------- <br>

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
