这篇文章主要为大家详细介绍了PHP对数组进行排序的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编小韵来看看吧!
经常,开发人员发现在PHP中使用这种数据结构对值或者数组元素进行排序非常有用。PHP提供了一些适合多种数组的排序函数,这些函数允许你在数组内部对元素进行排列,也允许用很多不同的方法对它们进行重新排序。在这篇文章中我们将讨论该排序中最重要的几个函数。代码如下:
<?php
$data = array(5,8,1,7,2);
sort($data);
print_r($data);
?>
代码如下:
Array ([0] => 1
[1] => 2
[2] => 5
[3] => 7
[4] => 8
)
代码如下:
<?php $data = array(5,8,1,7,2);rsort($data); print_r($data);
?>
代码如下:
Array ([0] => 8
[1] => 7
[2] => 5
[3] => 2
[4] => 1
)
代码如下:
<?php $data = array(“US” => “United States”, “IN” => “India”, “DE” => “Germany”, “ES” => “Spain”);ksort($data); print_r($data);
?>
代码如下:
Array ([DE] => Germany
[ES] => Spain
[IN] => India
[US] => United States
)
代码如下:
<?php $data = array(“US” => “United States”, “IN” => “India”, “DE” => “Germany”, “ES” => “Spain”);krsort($data); print_r($data);
?>
代码如下:
Array ([US] => United States
[IN] => India
[ES] => Spain
[DE] => Germany
)
代码如下:
<?php $data = array(“US” => “United States”, “IN” => “India”, “DE” => “Germany”, “ES” => “Spain”);asort($data); print_r($data);
?>
代码如下:
<?php $data = array(“US” => “United States”, “IN” => “India”, “DE” => “Germany”, “ES” => “Spain”);arsort($data); print_r($data);
?>
代码如下:
Array ([US] => United States
[ES] => Spain
[IN] => India
[DE] => Germany
)
代码如下:
<?php $data = array(“book-1″, “book-10″, “book-100″, “book-5″); sort($data);print_r($data);
natsort($data); print_r($data);?>
代码如下:
Array ([0] => book-1
[1] => book-10
[2] => book-100
[3] => book-5
)
Array
(
[0] => book-1
[3] => book-5
[1] => book-10
[2] => book-100
)
代码如下:
<?php $data = array(“book-1″, “book-10″, “book-100″, “book-5″);natsort($data); print_r(array_reverse($data));
?>
代码如下:
Array ([0] => book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
代码如下:
<?php $data = array(“joe@host.com”, “john.doe@gh.co.uk”, “asmithsonian@us.info”, “jay@zoo.tw”);usort($data, ‘sortByLen');
print_r($data); function sortByLen($a, $b) {
if (strlen($a) == strlen($b)) {
return 0;
} else {
return (strlen($a) > strlen($b)) ? 1 : -1;
}
}
?>
代码如下:
Array ([0] => jay@zoo.tw
[1] => joe@host.com
[2] => john.doe@gh.co.uk
[3] => asmithsonian@us.info
)
代码如下:
<?php $data = array(“book-1″, “book-10″, “book-100″, “book-5″); sort($data);print_r($data);
natsort($data); print_r($data);?>
代码如下:
Array ([0] => book-1
[1] => book-10
[2] => book-100
[3] => book-5
)
Array
(
[0] => book-1
[3] => book-5
[1] => book-10
[2] => book-100
)
代码如下:
<?php $data = array(“book-1″, “book-10″, “book-100″, “book-5″);natsort($data); print_r(array_reverse($data));
?>
代码如下:
Array ([0] => book-100
[1] => book-10
[2] => book-5
[3] => book-1
)
代码如下:
<?php $data = array(“joe@host.com”, “john.doe@gh.co.uk”, “asmithsonian@us.info”, “jay@zoo.tw”);usort($data, ‘sortByLen');
print_r($data); function sortByLen($a, $b) {
if (strlen($a) == strlen($b)) {
return 0;
} else {
return (strlen($a) > strlen($b)) ? 1 : -1;
}
}
?>
代码如下:
Array ([0] => jay@zoo.tw
[1] => joe@host.com
[2] => john.doe@gh.co.uk
[3] => asmithsonian@us.info
)
代码如下:
<?php $data = array(array(“id” => 1, “name” => “Boney M”, “rating” => 3),
array(“id” => 2, “name” => “Take That”, “rating” => 1),
array(“id” => 3, “name” => “The Killers”, “rating” => 4),
array(“id” => 4, “name” => “Lusain”, “rating” => 3),
); foreach ($data as $key => $value) {
$name[$key] = $value['name'];
$rating[$key] = $value['rating'];
}
array_multisort($rating, $name, $data); print_r($data);?>
代码如下:
Array ([0] => Array
(
[id] => 2
[name] => Take That
[rating] => 1
) [1] => Array
(
[id] => 1
[name] => Boney M
[rating] => 3
)
[2] => Array
(
[id] => 4
[name] => Lusain
[rating] => 3
)
[3] => Array
(
[id] => 3
[name] => The Killers
[rating] => 4
)
)
本文来自:http://www.q1010.com/173/13427-0.html
注:关于PHP对数组进行排序的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。