Home > php教程 > php手册 > body text

PHP函数asort()利用值排序的具体方式解析

WBOY
Release: 2016-06-13 11:06:42
Original
913 people have browsed it

我们在之前的文章中提到了如何运用ksort()函数实现按照关键字的排序方式。那么,今天我们就向大家重点介绍如何使用值对数组进行排序。在这一过程中我们将会用到Listing E的具体代码如下:

<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>("US" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "United States", "IN" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> <br>"India", "DE" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Germany", "ES" =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> "Spain");<br>asort($data); print_r($data);  </span></span></li>
<li class="">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

下面就是它的输出结果。请注意PHP函数asort()的结果与上面使用ksort()函数所得到的结果的不同——在这两种情况中,都是按字母顺序进行排序的,但是它们是根据数组的不同字段进行排序的。

同时,请注意关键字-值之间的联系会始终保持;它只是关键字-值对排序后的一种方式,排序并不会改变它们的对应关系。

Array ([DE] => Germany

[IN] => India

[ES] => Spain

[US] => United States

)

以上代码示例就是PHP函数asort()的具体应用,我们可以通过这个函数得到数组的值排序。


source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!