sort(): 将一个数组元素从低到高进行简单排序,这个函数既可以按数字大小排列也可以按字母顺序排列。
<code>Listing A <span><?php </span><span>$data</span> = <span>array</span>(<span>5</span>,<span>8</span>,<span>1</span>,<span>7</span>,<span>2</span>); sort(<span>$data</span>); print_r(<span>$data</span>); <span>?></span>输出结果如下所示: <span>Array</span> ([<span>0</span>] => <span>1</span>[<span>1</span>] => <span>2</span>[<span>2</span>] => <span>5</span>[<span>3</span>] => <span>7</span>[<span>4</span>] => <span>8</span>)</span></code>
rsort(): 对数组元素进行从高到低的倒排,同样可以按数字大小排列也可以按字母顺序排列。
<code><span><?php </span><span>$data</span> = <span>array</span>(<span>5</span>,<span>8</span>,<span>1</span>,<span>7</span>,<span>2</span>);rsort(<span>$data</span>); print_r(<span>$data</span>); <span>?></span>它的输出结果如下: <span>Array</span> ([<span>0</span>] => <span>8</span>[<span>1</span>] => <span>7</span>[<span>2</span>] => <span>5</span>[<span>3</span>] => <span>2</span>[<span>4</span>] => <span>1</span>)</span></code>
ksort(): 根据关键字进行排序的函数,同时,它在排序的过程中会保持关键字的相关性。
<code><span><?php </span><span>$data</span> = <span>array</span>(<span>"US"</span> => <span>"United States"</span>, <span>"IN"</span> => <span>"India"</span>, <span>"DE"</span> => <span>"Germany"</span>, <span>"ES"</span> => <span>"Spain"</span>); ksort(<span>$data</span>); print_r(<span>$data</span>); <span>?></span>它的输出结果如下: <span>Array</span> ([DE] => Germany [ES] => Spain [IN] => India [US] => United States )</span></code>
krsort(): 根据关键字对数组进行倒排。
<code><span><?php </span><span>$data</span> = <span>array</span>(<span>"US"</span> => <span>"United States"</span>, <span>"IN"</span> => <span>"India"</span>, <span>"DE"</span> => <span>"Germany"</span>, <span>"ES"</span> => <span>"Spain"</span>); krsort(<span>$data</span>); print_r(<span>$data</span>); <span>?></span>它的输出结果如下: <span>Array</span> ([US] => United States [IN] => India [ES] => Spain [DE] => Germany )</span></code>
asort(): 使用值排序来取代关键字排序,请注意关键字-值之间的联系会始终保持。
<code><span><?php </span><span>$data</span> = <span>array</span>(<span>"US"</span> => <span>"United States"</span>, <span>"IN"</span> => <span>"India"</span>, <span>"DE"</span> => <span>"Germany"</span>, <span>"ES"</span> => <span>"Spain"</span>); asort(<span>$data</span>); print_r(<span>$data</span>); <span>?></span>它的输出结果如下: <span>Array</span> ([DE] => Germany [IN] => India [ES] => Spain [US] => United States )</span></code>
arsort(): 根据值按字母表顺序进行倒排。
<code><span><?php </span><span>$data</span> = <span>array</span>(<span>"US"</span> => <span>"United States"</span>, <span>"IN"</span> => <span>"India"</span>, <span>"DE"</span> => <span>"Germany"</span>, <span>"ES"</span> => <span>"Spain"</span>); arsort(<span>$data</span>); print_r(<span>$data</span>); <span>?></span><span>Array</span> ([US] => United States [ES] => Spain [IN] => India [DE] => Germany )</span></code>
以上就介绍了php的排序函数,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。