In PHP, for sorting of 2-dimensional arrays, you can use the built-in functions usort()
and uasort()
to achieve it. Both functions can sort elements in an array, but the implementation is slightly different. The
usort()
function will sort the original array and return a Boolean value, while the uasort()
function will sort the original array but retain the key-value pairs The association between them means that no new index array is created. Below we will introduce how to use these two functions respectively.
usort()
Function
usort()
The first parameter of the function is the array that needs to be sorted, and the second parameter is a comparison Function, which is implemented by exchanging the positions of elements in the array that needs to be sorted and returning a Boolean value.
//示例数组$students $students = array( array("name" => "张三", "age" => 22), array("name" => "李四", "age" => 20), array("name" => "王五", "age" => 25) ); //比较函数cmp function cmp($a, $b){ if ($a["age"] == $b["age"]) { return 0; } return ($a["age"] <p>In the above code, we first define a 2-dimensional array $students containing three elements, each element contains two key-value pairs "name" and "age". </p><p>Next, we define a comparison function <code>cmp</code>, which determines the relative order between elements by comparing the values of two elements on the "age" key-value pair. If the "age" of the two elements is equal, 0 is returned; if the "age" of $a is less than the "age" of $b, then -1 is returned, indicating that $a is in front of $b; otherwise, 1 is returned, indicating that $a After $b. </p><p>Finally, we use the <code>usort()</code> function to sort the array $students according to the rules defined by the <code>cmp</code> function. From the output, you can see that the elements in the array have been sorted from small to large according to the "age" key-value pair. </p><h3> <code>uasort()</code>Function</h3><p><code>uasort()</code>The implementation of the function is similar to <code>usort()</code>, but different It retains the association between key-value pairs and does not create a new index array. Therefore, it requires a more complex comparison function that not only compares element sizes but also maintains the association of key-value pairs. </p><pre class="brush:php;toolbar:false">//示例数组$students $students = array( "stu1" => array("name" => "张三", "age" => 22), "stu2" => array("name" => "李四", "age" => 20), "stu3" => array("name" => "王五", "age" => 25) ); //比较函数cmp function cmp($a, $b){ if ($a["age"] == $b["age"]) { return 0; } return ($a["age"] <p>In the above code, we first define a 2-dimensional associative array $students containing three elements, each element contains two key-value pairs "name" and "age". The difference is that here we use the strings "stu1", "stu2" and "stu3" as the key values of the array instead of the previous numeric index. </p><p>The way to define the comparison function cmp is the same as <code>usort()</code>. The difference is that when we call the <code>uasort()</code> function, we pass the array $students and the comparison function <code>cmp</code> as parameters. This function will sort the array according to the <code>cmp</code> rules, and the sorted result retains the original joint key-value relationship. </p><p>Finally, we output the sorting results through the var_dump() function. As you can see, the output result is still an associative array, but the elements have been sorted from small to large according to the "age" key-value pair. </p><p>In addition to <code>usort()</code> and <code>uasort()</code>, PHP also provides a series of other array sorting functions, such as <code>asort()</code> , <code>arsort()</code>, <code>ksort()</code>, <code>krsort()</code>, etc. Developers can choose the appropriate function according to actual needs. </p>
The above is the detailed content of How to sort elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

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.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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
