如何运用PHP函数usort()实现自定义排序
在中进行数组排序的方式有许多种,包括按照值排序,按照关键字排序,自然语言排序等等。今天我们要教给大家的是运用PHP函数usort()来实现自定义的数组排序。你可以通过创建你自己的比较函数,并把它传递给PHP函数usort(),如果第一个参数比第二个参数“小”的话,比较函数必须返回一个比0小的数,如果第一参数比第二个参数“大”的话,比较函数应该返回一个比0大的数。
Listing I就是PHP函数usort()的一个例子,在这个例子中根据它们的长度对数组元素进行排序,最短的项放在最前面:
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li> <li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>("joe@host.com", "john.doe@gh.co.uk",<br>"asmithsonian@us.info", "jay@zoo.tw");usort($data, 'sortByLen'); </span></span></li> <li class=""><span>print_r($data); function sortByLen($a, $b) { </span></li> <li class="alt"><span>if (strlen($a) == strlen($b)) { </span></li> <li class=""><span>return 0; </span></li> <li class="alt"><span>} else { </span></li> <li class=""> <span>return (strlen($a) </span><span class="tag"><strong><font color="#006699">></font></strong></span><span> strlen($b)) ? 1 : -1; </span> </li> <li class="alt"><span>} </span></li> <li class=""><span>} </span></li> <li class="alt"> <span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
这样,就创建了我们自己的比较函数,这个函数使用PHP函数usort()比较每一个字符串的个数,然后分别返回1,0或-1.这个返回值是决定元素排列的基础。下面是它的输出结果:
Array ([0] => jay@zoo.tw
[1] => joe@host.com
[2] => john.doe@gh.co.uk
[3] => asmithsonian@us.info
)
希望大家能够通过这段示例代码学到PHP函数usort()的具体使用方式。

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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1
Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

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