The array_multisort function in PHP sorts multiple arrays or multidimensional arrays. The parameter array is treated as a column of a table and sorted by rows - this is similar to the function of SQL's ORDER BY clause. The first array is the main array to be sorted. If the rows (values) in the array are compared to be the same, they are sorted according to the size of the corresponding value in the next input array, and so on. Note: The number of array elements used as parameters should be consistent, otherwise an error will be reported.
Let’s first look at an example of sorting multiple arrays.
$array1 = array('one'=>'10','two'=>'20','three'=>'20','four'=>10); $array2 = array('one'=>'10','two'=>'30','three'=>'20','four'=>'1'); $array3 = array('one'=>'C','two'=>'A','three'=>'B','four'=>'F'); array_multisort($array1,$array2,$array3); print_r($array1);//Array ( [four] => 10 [one] => 10 [three] => 20 [two] => 20 ) print_r($array2);//Array ( [four] => 1 [one] => 10 [three] => 20 [two] => 30 ) print_r($array3);//Array ( [four] => F [one] => C [three] => B [two] => A )
In the above example, the first parameter array is first sorted (all arrays are sorted in ascending order by default), we can see that the The same values exist in an array (the key names 'one' and 'four' have the same key value, and the key names 'two' and 'three' have the same key value), so when sorting the same value in the first array, Sort by the size of the corresponding value in the next input array (the value of the second array 'four' is smaller than the value of 'one', so the value of four is sorted in front of one), and so on.
Looking at an example of changing the sort order.
$array1 = array('one'=>'10','two'=>'20','three'=>'20','four'=>10); $array2 = array('one'=>'10','two'=>'30','three'=>'20','four'=>'1'); $array3 = array('one'=>'C','two'=>'A','three'=>'B','four'=>'F'); array_multisort($array1,SORT_DESC,$array2,SORT_ASC,$array3); print_r($array1);//Array ( [three] => 20 [two] => 20 [four] => 10 [one] => 10 ) print_r($array2);//Array ( [three] => 20 [two] => 30 [four] => 1 [one] => 10 ) print_r($array3);//Array ( [three] => B [two] => A [four] => F [one] => C )
In this example, the first array is sorted in descending order, and when the same value is encountered, the second array is sorted according to the ascending value.
Note: If the sorted arrays are all associative arrays, the original key names will be retained. If there is an index array, the index will be re-established in order.
$array1 = array('one'=>'10',2=>'20',3=>'20',4=>10); $array2 = array('one'=>'10','2'=>'30','3'=>'20','four'=>'1'); $array3 = array('one'=>'C','2'=>'A','3'=>'B','four'=>'F'); array_multisort($array1,$array2,$array3); print_r($array1); //Array ( [0] => 10 [one] => 10 [1] => 20 [2] => 20 ) print_r($array2); //Array ( [four] => 1 [one] => 10 [0] => 20 [1] => 30 ) print_r($array3); //Array ( [four] => F [one] => C [0] => B [1] => A )
Multidimensional array sorting.
We usually have some multi-dimensional arrays that need to be sorted:
$guys = array( array('name'=>'jake', 'score'=>80, 'grade' =>'A'), array('name'=>'jina', 'score'=>70, 'grade'=>'A'), array('name'=>'john', 'score'=>70, 'grade' =>'A'), array('name'=>'ben', 'score'=>20, 'grade'=>'B') ); //例如我们想按成绩倒序排列,如果成绩相同就按名字的升序排列。 //这时我们就需要根据$guys的顺序多弄两个数组出来: $scores = array(80,70,70,20); $names = array('jake','jina','john','ben'); //然后 array_multisort($scores, SORT_DESC, $names, $guys); foreach($guys as $v){ print_r($v); echo "<br/>"; } /* Array ( [name] => jake [score] => 80 [grade] => A ) Array ( [name] => jina [score] => 70 [grade] => A ) Array ( [name] => john [score] => 70 [grade] => A ) Array ( [name] => ben [score] => 20 [grade] => B ) */
Related recommendations:
php for multiple Array or multi-dimensional array sorting function array_multisort()
10 recommended articles about array_multisort()
PHP array_multisort() function Notes on use
The above is the detailed content of Detailed explanation of array_multisort function of PHP array. For more information, please follow other related articles on the PHP Chinese website!

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa


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

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

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

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

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
