Home Backend Development PHP Tutorial php array_multisort() multi-array sorting method_PHP tutorial

php array_multisort() multi-array sorting method_PHP tutorial

Jul 13, 2016 pm 05:13 PM
array php exist sort array method

In php, array_multisort() can sort multiple arrays at one time, or sort multi-dimensional arrays according to a certain dimension or multiple dimensions. It returns TRUE if successful and FALSE if failed.

bool array_multisort (array ar1 [, mixed arg [, mixed ... [, array ...]]] )

Returns TRUE if successful, FALSE if failed.

array_multisort() can be used to sort multiple arrays at once, or to sort multi-dimensional arrays according to one or more dimensions.

Associative (string) key names remain unchanged, but numeric key names will be re-indexed.


Example 1. Sort multidimensional array

The code is as follows Copy code
 代码如下 复制代码

$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

$ar = array(

array("10", 11, 100, 100, "a"),
代码如下 复制代码

array(2) {
[0]=> array(5) {
    [0]=> string(2) "10"
    [1]=> int(100)
    [2]=> int(100)
    [3]=> int(11)
    [4]=> string(1) "a"
  }
  [1]=> array(5) {
    [0]=> int(1)
    [1]=> int(3)
    [2]=> string(1) "2"
    [3]=> int(2)
    [4]=> int(1)
  }
}

array( 1, 2, "2", 3, 1)

);

array_multisort($ar[0], SORT_ASC, SORT_STRING,

                    $ar[1], SORT_NUMERIC, SORT_DESC);

var_dump($ar);
 代码如下 复制代码

$ar = array(
array("10", 11, 100, 100, "a"),
array( 1, 2, "2", 3, 1)
);
array_multisort($ar[0], SORT_ASC, SORT_STRING,
$ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?> 

?> In this example, after sorting, the first array will transform to "10", 100, 100, 11, "a" (it was sorted as strings in ascending order). The second will contain 1, 3, "2 ", 2, 1 (sorted as numbers, in descending order).
The code is as follows Copy code
array(2) { [0]=> array(5) { [0]=> string(2) "10" [1]=> int(100) [2]=> int(100) [3]=> int(11) [4]=> string(1) "a" } [1]=> array(5) { [0]=> int(1) [1]=> int(3) [2]=> string(1) "2" [3]=> int(2) [4]=> int(1) } }
After sorting in this example, the first array will contain 10, 100, 100, "a" (as ascending order of strings), and the second array will contain 1, 3, "2", 1 (as numerically descending order). Example 2. Sorting multi-dimensional array
The code is as follows Copy code
$ar = array(<🎜> array("10", 11, 100, 100, "a"),<🎜> array( 1, 2, "2", 3, 1)<🎜> );<🎜> array_multisort($ar[0], SORT_ASC, SORT_STRING,<🎜>                      $ar[1], SORT_NUMERIC, SORT_DESC);<🎜> var_dump($ar);<🎜> ?>

In this example, after sorting, the first array will become "10", 100, 100, 11, "a" (treated as strings in ascending order). The second array will contain 1, 3, "2", 2, 1 (treated as numbers in descending order).

 代码如下 复制代码
array(2) {
  [0]=> array(5) {
    [0]=> string(2) "10"
    [1]=> int(100)
    [2]=> int(100)
    [3]=> int(11)
    [4]=> string(1) "a"
  }
  [1]=> array(5) {
    [0]=> int(1)
    [1]=> int(3)
    [2]=> string(1) "2"
    [3]=> int(2)
    [4]=> int(1)
  }
}
 

Example 3 Let’s take a comprehensive look at an example commonly used in applications.

 代码如下 复制代码

header('Content-Type: text/html; charset=utf-8');
echo '
'; 
//原始数组格式
$array = array(
'key1' => array(
'item1' => '65',
'item2' => '35',
'item3' => '84',
),
'key2' => array(
'item1' => '24',
),
'key3' => array(
'item1' => '38',
'item3' => '45',
),
);
//要排序的键
//按照数组中的 item1进行排序
//你也可以换成item2
$sort = 'item1';
foreach($array as $k => $v)
{
$newArr[$k] = $v[$sort];
}
//这个函数如果执行正确他会直接改变原数组键值的顺序
//如果执行失败,那么他会返回 bool(false)
array_multisort($newArr,SORT_DESC, $array);
var_dump($array);
//---------------------排序后的数组打印效果 开始--------------------
array(3) {
["key1"]=>
array(3) {
["item1"]=>
string(2) "65"
["item2"]=>
string(2) "35"
["item3"]=>
string(2) "84"
}
["key3"]=>
array(2) {
["item1"]=>
string(2) "38"
["item3"]=>
string(2) "45"
}
["key2"]=>
array(1) {
["item1"]=>
string(2) "24"
}
}
//---------------------排序后的数组打印效果 结束---------------------

For a detailed explanation of the array_multisort() function, please refer to http://www.bKjia.c0m/phper/php-function/39192.htm

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629150.htmlTechArticleIn php, array_multisort() can sort multiple arrays at one time, or sort them according to a certain dimension or multiple dimensions. Sort multidimensional arrays, returning TRUE if successful, and FALSE if failed. ...
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 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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles